简体   繁体   English

Selenium Python - 无法通过 Xpath 选择

[英]Selenium Python - unable to select by Xpath

I'm having an issue clicking an icon/link in a table.我在单击表格中的icon/link时遇到问题。 I've tried both find_element_by_xpath and find_elements_by_Xpath - no luck with either.我已经尝试过find_element_by_xpathfind_elements_by_Xpath - 两者都不走运。 I forced the wait as I was getting some issues with the element not being found.我强迫等待,因为我遇到了一些找不到元素的问题。

I've highlighted the icon in the Table row, with the red box.我在表格行中突出显示了带有红色框的图标。 in this image.在这张图片中。 Website网站

Also found the Xpath but can't seem to get it to work, the icon is clickable on the webpage.还找到了 Xpath,但似乎无法使其正常工作,该图标可在网页上单击。 Xpath Xpath

My code is below:我的代码如下:

driver.implicitly_wait(7)
tr = driver.find_element_by_xpath('//*[@id="AthleteTheme_wt6_block_wtMainContent_wt9_wtClassTable_ctl05_AthleteTheme_wt221_block_wtIconSvg_Svg"]/svg/use')

tr.click()

Thanks谢谢

The Element is in an svg tag.元素位于svg标签中。 And there is a different syntax for the same.同样有不同的语法。 Links to refer - Link1 , Link2参考链接 - Link1 , Link2

To access svg tag elements the syntax would something like this:要访问svg标签元素,语法如下:

//*[local-name()='svg']

As per the screen shot the xpath for the Elements would be:根据屏幕截图,元素的 xpath 将是:

//span[@id="AthleteTheme_wt6_block_wtMainContent_wt9_wtClassTable_ctl05_AthleteTheme_wt221_block_wtIconSvg_Svg"]/*[local-name()='svg']/*[local-name()='use']

You can not directly use // to locate an SVG element.您不能直接使用 // 来定位 SVG 元素。 They are one of the special tags.它们是特殊标签之一。

Always use //*[name()='svg'] or //*[local-name()='svg'] to locate them.始终使用//*[name()='svg']//*[local-name()='svg']来定位它们。

Based on the HTML that you've shared, Please use the below xpath :根据您共享的 HTML,请使用以下 xpath :

//a[@class='svgContainer']//child::span//*[name()='svg' and starts-with(@id,'AthleteTheme')]//*[name()='use']

** Please check in the dev tools (Google chrome) if we have unique entry in HTML DOM or not. ** 如果我们在HTML DOM是否有唯一条目,请检查dev tools (谷歌浏览器)。

Steps to check:检查步骤:

Press F12 in Chrome -> go to element section -> do a CTRL + F -> then paste the xpath and see, if your desired element is getting highlighted with 1/1 matching node. Press F12 in Chrome -> 转到element部分 -> 执行CTRL + F -> 然后粘贴xpath并查看您想要的element是否被1/1匹配节点突出显示

Code trial 1 :代码试用1:

time.sleep(5)
driver.find_element_by_xpath("//a[@class='svgContainer']//child::span//*[name()='svg' and starts-with(@id,'AthleteTheme')]//*[name()='use']").click()

Code trial 2 :代码试用2:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='svgContainer']//child::span//*[name()='svg' and starts-with(@id,'AthleteTheme')]//*[name()='use']"))).click()

Code trial 3 :代码试用3:

time.sleep(5)
button = driver.find_element_by_xpath("//a[@class='svgContainer']//child::span//*[name()='svg' and starts-with(@id,'AthleteTheme')]//*[name()='use']")
driver.execute_script("arguments[0].click();", button)

Code trial 4 :代码试用4:

time.sleep(5)
button = driver.find_element_by_xpath("//a[@class='svgContainer']//child::span//*[name()='svg' and starts-with(@id,'AthleteTheme')]//*[name()='use']")
ActionChains(driver).move_to_element(button).click().perform()

Imports :进口:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM