2017-05-04 106 views
0

This是一个电子商务网站,我想抓取的链接。我正在寻找一种方法来点击最有帮助的,积极的,消极的,最新的以及通过认证的买家部分,并刮取价值观。抬起头来,这不是一个按钮,所以ActionChains和Javascript代码无法正常工作。硒蟒蛇不能点击元素在点(X,Y),而不是其他元素将收到点击(这里的元素既不是按钮,也不是链接)

我想从一个或者通过点击或与任何其他方法转移到另一个。我尝试通过使用JavaScript执行者和ActionChains,但我无法得到它。

因为我这个XPath是:

path = '//div[@class="o5jqS-"]/div[X]//div[contains(@class,"_3MuAT6")]' 

实际上返回元素。在“X”值将替换在1环到5 1,表示“最有价值”,5表示“通过Certfied买家”

我的代码如下:

for j in range(0,5): 

    new_xpath = xpath_hash["FirstPageReviews"]["TitleOfReviewType"].replace("[X]", "[" + str(j + 1) + "]") 
    new_xpath1 = xpath_hash["FirstPageReviews"]["TitleElement"].replace("[X]", "[" + str(j + 1) + "]") 
    title_element = driver.find_element_by_xpath(new_xpath1) 
    driver.execute_script("arguments[0].click();", title_element) 
    #ActionChains(driver).move_to_element(title_element).click().perform() 
+0

使用动作类先移动到该元素,然后单击它 – kushal

+0

我希望这将有助于肯定https://stackoverflow.com/a/45369987/6008000谢谢 –

+0

[Python&Selenium - 未知错误:元素是n可点击(663,469)。其他元件将接收点击:](https://stackoverflow.com/questions/36033859/python-selenium-unknown-error-element-is-not-clickable-at-point-663-469) –

回答

0

你可以使用我的代码页面对象的基础上,我曾尝试这个工作

from selenium import webdriver 
from selenium.webdriver.common.by import By 
from selenium.webdriver.support.wait import WebDriverWait 
from selenium.common.exceptions import NoSuchElementException 
from selenium.common.exceptions import NoAlertPresentException 
from selenium.webdriver.common.action_chains import ActionChains 
from selenium.webdriver.support import expected_conditions as EC 

from selenium.webdriver.chrome.options import Options 
from selenium.webdriver.support.ui import Select 


class SeleniumBaseClass(object): 
    def __init__(self,driver): 
     self.driver = driver 
    def open(self,URL): 
     self.driver.get(URL) 
    def driverURLChange(self,URL): 
     print("change URL" + URL) 
     self.driver.get(URL) 
    def currentUrl(self): 
     print("URL " + self.driver.current_url) 
     return self.driver.current_url 

    def locateElement(self, loc): 
     try: 
      print(loc) 
      element = WebDriverWait(self.driver,10).until(EC.visibility_of_element_located(loc)) 
      return element 
     except: 
      print ("cannot find {0} element".format(loc)) 
     return None 

    def waitForElementInvisible(self,loc): 
     #load-spinner 
     try: 
      element = WebDriverWait(self.driver,10).until(EC.invisibility_of_element_located(loc)) 
      return True 
     except: 
      print ("cannot invisibility_of_element {0} element".format(loc)) 
     return False    

    def send_key_with_Element(self,loc,value): 
     self.locateElement(loc).clear() 
     self.locateElement(loc).send_keys(value) 
    def click_with_Element(self,loc): 
     self.locateElement(loc).click() 
    def clickElementsBySendKey(self,loc,value): 
     self.locateElement(loc).send_keys(value) 




customdriver = SeleniumBaseClass(webdriver.Chrome()) 

customdriver.open("https://www.flipkart.com/sony-mdr-zx110-wired-headphones/p/itmehuh6zm9s7kgz?pid=ACCDZRSEYPFHAT76&srno=s_1_1&otracker=search&lid=LSTACCDZRSEYPFHAT76TANM1F&qH=a684a6245806d98f") 
HelpfulTab = (By.XPATH,"//div[contains(text(),'Most Helpful')]") 
PositiveTab = (By.XPATH,"//div[contains(text(),'Positive')]") 

customdriver.click_with_Element(PositiveTab) 
+0

上述溶液是不工作 – Bharathi

+0

我需要更改代码以使用visibility_of_element_located来定位元素,元素需要显示的时间 –

相关问题