錯誤結果,因爲我無法通過xpath獲取正確的元素。 Web句柄是否改變? 我可以點擊圖片嗎?我不明白如何解釋錯誤。Selenium如何點擊不帶鏈接的年齡驗證
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
driver = webdriver.Chrome()
#browser.get('https://ebs.lcb.state.pa.us/OA_HTML/AppsLocalLogin.jsp')
driver.get('http://www.finewineandgoodspirits.com')
assert 'Fine Wine & Good Spirits: Shop Online for Wine and Spirits in Pennsylvania' in driver.title
browser.find_element_by_xpath("xpath=//div[@id='ageVerify']/img[@alt='Yes']").click()
InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression xpath=//div[@id='ageVerify']/img[@alt='Yes'] because of the following error: TypeError: Failed to execute 'evaluate' on 'Document': The result is not a node set, and therefore cannot be converted to the desired type. (Session info: chrome=59.0.3071.115) (Driver info: chromedriver=2.26.436362 (5476ec6bf7ccbada1734a0cdec7d570bb042aa30),platform=Windows NT 6.1.7601 SP1 x86_64)
首先,更新你的問題與HTML剪斷其中元素你的目標所在。其次,使用不同的'WebElement'定位器。打開瀏覽器控制檯並開始使用jQuery來驗證選擇器是否有效(例如:'$ x(// div [@ id ='ageVerify']/img [@ alt ='Yes'])''或$( div#ageVerify img [alt =「是」])'爲CSS路徑)。你的錯誤不能比這更詳細。 ('無法找到一個元素與xpath exp ...',這意味着,你的選擇器不好) – iamdanchiv