Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.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 3.x 如何在python/selenium中为动态元素编写xpath_Python 3.x_Selenium_Selenium Webdriver - Fatal编程技术网

Python 3.x 如何在python/selenium中为动态元素编写xpath

Python 3.x 如何在python/selenium中为动态元素编写xpath,python-3.x,selenium,selenium-webdriver,Python 3.x,Selenium,Selenium Webdriver,我正在尝试单击一个名为“源代码管理”的选项卡,该选项卡有一个动态生成的id。在联机查看时,尝试了以下方法,但仍然没有使用 尝试了以下xpath: //li[@class="menu-item"]/a/strong[text(),Source Control]') //li[@class="menu-item"]//a//text()[preceding-sibling::strong][normalize-space()!=''] //li//a[starts-with(id,"aui-uid-

我正在尝试单击一个名为“源代码管理”的选项卡,该选项卡有一个动态生成的id。在联机查看时,尝试了以下方法,但仍然没有使用

尝试了以下xpath:

//li[@class="menu-item"]/a/strong[text(),Source Control]')
//li[@class="menu-item"]//a//text()[preceding-sibling::strong][normalize-space()!='']
//li//a[starts-with(id,"aui-uid-")]/strong[text(),Source Control]
我正在使用的代码

Sourcecontrol=driver.find_element_by_xpath('//li[@class="menu-item"]/a/strong[text(),Source Control]')
if not Sourcecontrol:
    print("No element found")  
else:
    Sourcecontrol.click();
HTML


使用以下xpath。它将搜索锚点元素内的子字符串。请尝试使用以下选项

try:
 Sourcecontrol=driver.find_element_by_xpath('//li[@class="menu-item"]/a[contains(.,"Source Control")]')
 Sourcecontrol.click();
except:
 print("No element found")

try:
 Sourcecontrol=driver.find_element_by_xpath('//li[@class="menu-item"]/a[contains(.,"Source Control")]')
 Sourcecontrol.click();
except:
 print("No element found")
if len(driver.find_elements_by_xpath('//li[@class="menu-item"]/a[contains(.,"Source Control")]'))>0:
   Sourcecontrol = driver.find_element_by_xpath('//li[@class="menu-item"]/a[contains(.,"Source Control")]')
   Sourcecontrol.click();
else:
    print("No element found")