简体   繁体   English

selenium-Python,如何单击注销图像按钮

[英]selenium-Python, how to click the logout image button

the html is below: html如下:

<li class="logout"> <a href="#/logout"><img src="resources/img/logout.png"/></a></li>

python is python是

driver.find_element_by_xpath("//img[contains(@src='resources/images/logout.png')]/parent::a").click()

it giving error as below 它给出了如下错误

   driver.find_element_by_xpath("//img[contains(@src='resources/images/logout.png')]/parent::a").click()
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 221, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 681, in find_element
    {'using': by, 'value': value})['value']
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 164, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 164, in check_response
    raise exception_class(message, screen, stacktrace)
NoSuchElementException: Message: u'Unable to locate element: {"method":"xpath","selector":"//img[contains(@src=\'resources/images/logout.png\')]/parent::a"}' ; Stacktrace: 
    at FirefoxDriver.prototype.findElementInternal_ (file:///tmp/tmpu3t8lf/extensions/fxdriver@googlecode.com/components/driver_component.js:8860)
    at fxdriver.Timer.prototype.setTimeout/<.notify (file:///tmp/tmpu3t8lf/extensions/fxdriver@googlecode.com/components/driver_component.js:396) 

I'd rather use css selector: 我宁愿使用css选择器:

driver.find_element_by_css_selector('li.logout>a').click()

See find_element_by_css_selector . 请参见find_element_by_css_selector


If you want use xpath, use following xpath expression (There's a typo in the code: images -> img ): 如果你想使用xpath,请使用以下xpath表达式(代码中有一个拼写错误: images - > img ):

driver.find_element_by_xpath('//a[img/@src="resources/img/logout.png"]').click()

请尝试driver.find_element_by_xpath("//img[@src='resources/images/logout.png']/parent::a").click()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 selenium-python 单击特定按钮 - How to click a specific button using selenium-python 如何单击 selenium-python 中的阴影框? - How to click on the shadowbox in selenium-python? Selenium-Python:无法单击选择中的选项 - Selenium-Python: unable to click an option in a select 如何使用find_element_by_xpath或selenium-python API从以下HTML代码中选择“开始”按钮并单击它 - How to select 'Start' button and click it from following HTML code by using find_element_by_xpath or selenium-python API Selenium-python/ 尝试通过选择上传按钮并将图像路径作为文本发送来上传图像,但我什么也没得到 - Selenium-python/ trying to upload an image by selecting the upload button and sending the path of the image as text but I get nothing Selenium Python 代码单击注销按钮? - Selenium Python Code to click on logout button? Selenium-Python-如何减少NoSuchElementException的时间 - Selenium-Python - How to reduce time for NoSuchElementException 如何使用 Selenium-Python 抓取 dropdwon 菜单? - How to scrape a dropdwon menu with Selenium-Python? 如何在Heroku上部署Selenium-python - How to deploy Selenium-python on Heroku selenium-python 点击坐标:“+ 不支持的操作数类型” - selenium-python click on coordinates: "unsupported operand type(s) for +"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM