我在單擊icon/link表中的 時遇到問題。我已經嘗試過兩者find_element_by_xpath并且find_elements_by_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()
謝謝
uj5u.com熱心網友回復:
元素位于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']
uj5u.com熱心網友回復:
您不能直接使用 // 來定位 SVG 元素。它們是特殊標簽之一。
始終使用//*[name()='svg']或//*[local-name()='svg']來定位它們。
根據您共享的 HTML,請使用以下 xpath :
//a[@class='svgContainer']//child::span//*[name()='svg' and starts-with(@id,'AthleteTheme')]//*[name()='use']
** 請檢查dev tools(谷歌瀏覽器)我們是否有唯一的條目HTML DOM。
檢查步驟:
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/324932.html
