Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Selenium:如何使用css选择器单击下拉菜单中的链接?_Python_Css_Selenium - Fatal编程技术网

Python Selenium:如何使用css选择器单击下拉菜单中的链接?

Python Selenium:如何使用css选择器单击下拉菜单中的链接?,python,css,selenium,Python,Css,Selenium,HTML如下所示: <span class="MenuIcons searchButton"></span> ... (some stuff) <a data-bind="" url="/ParagonLS/Search/Property.mvc/Index/1" tabdescription="RESIDENTIAL" subtabdescription="Criteria" subtabmaxallowed="3" targetex="" rel

HTML如下所示:

<span class="MenuIcons searchButton"></span>
    ... (some stuff)
    <a data-bind="" url="/ParagonLS/Search/Property.mvc/Index/1" tabdescription="RESIDENTIAL" subtabdescription="Criteria" subtabmaxallowed="3" targetex="" rel="" class=" SearchByClass1 " subtabgroup="true" subtabgroupadd="true" subtabstartindex="0" fullwindow="False" hideaddressbar="False">TEXT</a>

但由于span是一个下拉菜单,我需要进入内部元素,但不知道如何操作,因为它的类名周围有空格。我该怎么办?

我建议您改用
xpath
,因为
类包含空格

//a[contains(@class,'SearchByClass1')]
基于文本的搜索也是另一种可能性

//a[.='TEXT']
编辑 执行
javascript
,因为元素按照OP的注释隐藏

test = driver.execute_script("return document.querySelector(\"a[class*='SearchByClass1']\").innerHTML;");
print(test)
印刷品

正文


您可以这样做,然后单击链接。

我尝试使用xpath,但由于类名中有空格,因此出现了错误。如果您能告诉我如何处理空格,这是一个可能的解决方案。您能通过
链接文本
进行选择吗?我可以,但我需要找到一种方法将其取消隐藏。通过单击元素与网站进行交互,然后出现下拉菜单,允许单击元素。该属性最初被隐藏时是否有效?不要这样认为。那是隐藏的吗?它隐藏在元素下面。这就是为什么我要先打开它,然后才能为聊天计划执行
javascript
?Thanx:)我得到“无法定位元素:{”方法“:“链接文本”,“选择器“:“文本”}”error@PythonNoob链接文本的确切内容是什么?谢谢您的更正。我认为这是非常重要的cleaner@Saifur我猜他以前错过了点击动作。我也错过了:)
test = driver.execute_script("return document.querySelector(\"a[class*='SearchByClass1']\").innerHTML;");
print(test)
import time
driver.find_element_by_css_selector(".MenuIcons.searchButton").click()
time.sleep(1)
driver.find_element_by_partial_link_text("TEXT").click()