我在Python
中使用Selenium
在href
XPath
中找到了一个问题定位元素。我使用XPath
控制台检查了我的Chrome
是否正确,我对它返回的结果感到满意。
我使用以下内容验证XPath
控制台上的Chrome
:
$x("//li[@id='result_0']//a[@href='https://www.amazon.co.uk/HuntGold-Plastic-Outdoor-Carrier-Container/dp/B00R7DCPWO/ref=sr_1_1?ie=UTF8&qid=1476540708&sr=8-1&keywords=banana+holder#customerReviews']")
然后我在XPath
Python
代码中使用了相同的Selenium
:
path = "//li[@id='result_0]//a[@href='https://www.amazon.co.uk/HuntGold-Plastic-Outdoor-Carrier-Container/dp/B00R7DCPWO/ref=sr_1_1?ie=UTF8&qid=1476540708&sr=8-1&keywords=banana+holder#customerReviews']"
reviews = driver.find_element_by_xpath(path).text
我收到以下错误消息:
reviews = driver.find_element_by_xpath(reviews_path).text
File "/Users/Paulius/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 293, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "/Users/Paulius/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 752, in find_element
'value': value})['value']
File "/Users/Paulius/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "/Users/Paulius/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 192, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element
(Session info: chrome=53.0.2785.143)
(Driver info: chromedriver=2.16.333243 (0bfa1d3575fc1044244f21ddb82bf870944ef961),platform=Mac OS X 10.12.0 x86_64)
有谁知道如何在href
XPath
工作中Python
Selenium
?