Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.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 从元素列表中获取Id/Xpath以访问它取决于变量_Python_Selenium_Browser Automation - Fatal编程技术网

Python 从元素列表中获取Id/Xpath以访问它取决于变量

Python 从元素列表中获取Id/Xpath以访问它取决于变量,python,selenium,browser-automation,Python,Selenium,Browser Automation,强调文本 嘿,伙计们 我在Python中有一个变量(字符串),它是14个类别(电子、服装…)中的一个。现在我想单击web中的特定元素,该元素等于我的变量。 网络中的所有类别都有相同的类名 for elm in driver.find_elements_by_class_name('category-selection-list-item-link'): print(elm.xpath) 在这里,我有一个包含所有类别字符串的列表。现在,我需要每个用户的特定xpath或id,以便能够访问它 我希望

强调文本

嘿,伙计们

我在Python中有一个变量(字符串),它是14个类别(电子、服装…)中的一个。现在我想单击web中的特定元素,该元素等于我的变量。 网络中的所有类别都有相同的类名

for elm in driver.find_elements_by_class_name('category-selection-list-item-link'):
print(elm.xpath)
在这里,我有一个包含所有类别字符串的列表。现在,我需要每个用户的特定xpath或id,以便能够访问它

我希望你能理解我的问题,并能提供帮助!
非常感谢。

您所要做的就是将类别传递给下面的xpath

//*[@class='category-selection-list-item-link'][normalize-space(.)='category_name_goes_here']
现在在脚本中使用此xpath,如下所示

# below script will click on Jobs category item.
driver.find_element_by_xpath("//*[@class='category-selection-list-item-link'][normalize-space(.)='Jobs']").click