简体   繁体   English

Selenium在Python中使用xpath选择一个元素

[英]Selenium select an element with xpath in Python

I want to navigate to /yandsearch?... by clicking on the related links. 我想通过单击相关链接导航到/yandsearch?... There are two links in the page: 页面中有两个链接:

<a class="button button_theme_pseudo button_pseudo-pressed_yes button_size_m button_counter_yes button_focus_no b-link b-link_pseudo_yes pager__button pager__button_kind_page i-bem button_js_inited" onmousedown="w(this, '405.88');" href="/yandsearch?lr=157&text=landsad&p=3" role="button" ondblclick="return {"button":{"counter":"w(this, '405.88');"},"b-link":{}}">
<a class="button button_theme_pseudo button_pseudo-pressed_yes button_size_m button_counter_yes button_focus_no b-link b-link_pseudo_yes pager__button pager__button_kind_page i-bem" onmousedown="w(this, '405.88');" href="/yandsearch?lr=157&text=landsad&p=3" role="button" ondblclick="return {"button":{"counter":"w(this, '405.88');"},"b-link":{}}">

I try to select the first using: 我尝试选择第一个使用:

driver.find_element_by_xpath(str('//*[contains(@href,"yandsearch?lr=157&text=landsad&p=3")] and [contains(@class,"button_js_inited")]')

It should click on the first item, but find_element_by_xpath can't find it. 它应该单击第一项,但find_element_by_xpath找不到。
I get the error: 我得到错误:

    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidSelectorException: Message: 'The given selecto......

I want to click on page 4 at this link yandex.by/yandsearch 我想单击此链接yandex.by/yandsearch第4页

I tried it also: 我也尝试过:

//*[contains(@href,"yandsearch?lr=157&text=landsad&p=3") and contains(@class,"button_js_inited")]

writes an error: 写入错误:

self.error_handler.check_response(response) File "C:\Python34\lib\site- packages\selenium\webdriver\remote\errorhandler.py" , 
line 164, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: 
Message: 'Unable to locate element: {"method":"xpath","selector":"//* [contains(@href,\\"yandsearch?lr=157&tex t=landsad&p=3\\") and contains(@class,\\"button_js_inited\\")]"}';

Your XPath is invalid, both criteria should be placed within the same square-brackets (formatted for readiblity) : 您的XPath无效,两个条件都应放在相同的方括号中(为方便起见而设置):

//*[
        contains(@href,"yandsearch?lr=157&text=landsad&p=3") 
            and 
        contains(@class,"button_js_inited")
   ]

Not formatted version : 未格式化的版本:

//*[contains(@href,"yandsearch?lr=157&text=landsad&p=3") and contains(@class,"button_js_inited")]

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

相关问题 带xpath的Python Selenium的select元素时出现InvalidSelectorException - InvalidSelectorException when select element for Python Selenium with xpath 无法在 selenium 和 Z23EEEB4347BDD26BFC6B7EE9A3B75 中使用 xpath 的元素 select - unable to select an element using xpath in selenium and python 如何在Selenium和Python中选择元素槽xpath? - How to select element trough xpath in Selenium and Python? Select Selenium、Python 中的一个元素,而不是使用 ZB6454D4986313710A1891ZED1CEA - Select an element in Selenium, Python, not by using XPath 无法通过Selenium Webdriver python中的XPath选择元素 - Unable to select an element via XPath in selenium Webdriver python 无法在Python中使用Selenium从下拉菜单中通过xpath选择元素 - Unable to select element by xpath from dropdown menu with Selenium in Python 无法在 select by XPATH Selenium ZA7F5F35426B9278211FCZ23135B 之后单击元素 - Unable to click element after select by XPATH Selenium Python 如何将 select 元素在 Selenium 上使用 XPATH 语法用于 Python? - How to select element using XPATH syntax on Selenium for Python? 在Python、select HTML页面元素内容中使用selenium与xpath? - Using selenium in Python, select HTML page element content with xpath? Python:Selenium“没有这样的元素”XPath 或 ID - Python: Selenium "no such element" XPath or ID
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM