2017-09-27 15 views
0

我想通過xpath找到按鈕元素,當我在chrome中鍵入它時發現該按鈕元素,但該腳本給了我無屬性id錯誤。我嘗試切換到iframe和框架,我用webdriver.wait等待按鈕元素顯示,並沒有這些工作。我還想循環點擊第一個按鈕,如果它顯示「Follow」,然後移動到下一個按鈕,如果它顯示「Follow」。腳本在Chrome上運行,我試圖在instagram上執行此操作Html hereXpath存在於chrome中,但不存在於我的腳本中。沒有這樣的元素:無法找到元素:

popup = browser.find_element_by_xpath('//button[@class="_qv64e _gexxb _4tgw8 _njrw0"]') 

ActionChains(browser)\ 
.move_to_element(popup).click()\ 
.perform() 

File "/Users/trevaroneill/PycharmProjects/Insta/instafollow.py", line 91, in <module> popup = browser.find_element_by_xpath('//button[@class="_qv64e _gexxb _4tgw8 _njrw0"]')

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//button[@class="_qv64e _gexxb _4tgw8 _njrw0"]"} 
+0

請分享網址 – iamsankalp89

+0

你看到的錯誤是正常的,因爲名單沒有名爲id – iamsankalp89

+0

你可以給一些HTML屬性碼?腳本也運行在chrome上呢? – yong

回答

0

你得到在列表中所需的元素。

變化 popup = browser.find_elements_by_xpath('//button[@class="_qv64e _gexxb _4tgw8 _njrw0"]')

popup = browser.find_element_by_xpath('//button[@class="_qv64e _gexxb _4tgw8 _njrw0"]')

find_element而不是find_elements

+0

不起作用stil,我得到的錯誤:selenium.common.exceptions.NoSuchElementException:消息:沒有這樣的元素:無法找到元素:{「method」:「xpath」,「selector」:「// button [@ class =「_qv64e _gexxb _4tgw8 _njrw0」]「}(會話信息:chrome = 61.0.3163.100) – Trevar

+0

然後xpath出現問題。 確保在執行代碼時使用類別爲「_qv64e _gexxb _4tgw8 _njrw0」的按鈕(使用任何調試器)。 此外,您可以在「控制檯」選項卡中的任何瀏覽器的檢查模式下測試xpath。輸入控制檯輸入'document.getElementsByClassName()' –

1

由於您使用find_element 小號,你應該寫寧:

ActionChains(browser)\ 
.move_to_element(popup[0]).click()\ 
.perform() 

,以訪問由find_elements返回列表中的第一個元素。

問題是如果你有幾個webelements被你的xpath選中,在這種情況下,它不確定第一個是你實際瞄準的那個webelements。我會建議你使用find_element如果沒有特別的原因,你正在使用find_elements

+0

不工作stil,我得到錯誤:selenium.common.exceptions.NoSuchElementException:消息:沒有這樣的元素:無法找到元素:{「method」:「xpath 「,」selector「:」//按鈕[@class =「_qv64e _gexxb _4tgw8 _njrw0」]「} (會話信息:chrome = 61.0.3163.100) – Trevar

+0

我的猜測在這種情況下,是一個不正確的xpath,幾種可能性: 1)_qv64e _gexxb _4tgw8 _njrw0看起來像一個動態生成的類名。確保它在調試測試時仍然存在,正如@YoavGaudin所建議的那樣。 2)嘗試重新表達xpath。使用與class屬性不同的somme,或按鈕節點。 3)當您嘗試訪問元素時,是否已完全下載DOM?如果不是,隱含的等待可能會有所幫助。 最好的辦法是將html源代碼或鏈接指向正在測試的頁面。 –

相關問題