![](/img/trans.png)
[英]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.