繁体   English   中英

Selenium Python - 无法通过 Xpath 选择

[英]Selenium Python - unable to select by Xpath

我在单击表格中的icon/link时遇到问题。 我已经尝试过find_element_by_xpathfind_elements_by_Xpath - 两者都不走运。 我强迫等待,因为我遇到了一些找不到元素的问题。

我在表格行中突出显示了带有红色框的图标。 在这张图片中。 网站

还找到了 Xpath,但似乎无法使其正常工作,该图标可在网页上单击。 Xpath

我的代码如下:

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()

谢谢

元素位于svg标签中。 同样有不同的语法。 参考链接 - Link1 , Link2

要访问svg标签元素,语法如下:

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

根据屏幕截图,元素的 xpath 将是:

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

您不能直接使用 // 来定位 SVG 元素。 它们是特殊标签之一。

始终使用//*[name()='svg']//*[local-name()='svg']来定位它们。

根据您共享的 HTML,请使用以下 xpath :

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

** 如果我们在HTML DOM是否有唯一条目,请检查dev tools (谷歌浏览器)。

检查步骤:

Press F12 in Chrome -> 转到element部分 -> 执行CTRL + F -> 然后粘贴xpath并查看您想要的element是否被1/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()

代码试用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()

代码试用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)

代码试用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()

进口:

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