2017-05-04 86 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來定位元素,元素需要顯示的時間 –

相關問題