[英]unable to select an element using xpath in selenium and python
[英]Selenium Python - unable to select by Xpath
我在单击表格中的icon/link
时遇到问题。 我已经尝试过find_element_by_xpath
和find_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 元素。 它们是特殊标签之一。
始终使用//*[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.