Python 3.x 如何在python/selenium中为动态元素编写xpath
我正在尝试单击一个名为“源代码管理”的选项卡,该选项卡有一个动态生成的id。在联机查看时,尝试了以下方法,但仍然没有使用 尝试了以下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-
//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")