我怎樣才能用Selenium從串列元素中選擇這個按鈕并點擊它? 這就是HTML
<li class="ml1 sel"/span>>
<a href="#__about. htm" id="about_page" onclick="return menuClick(this)。 " class="T sel">
<span>關于我們</span>/span>
</a>/span>
</li>
我曾嘗試使用xpath css selector id和class name。
但總是得到錯誤 selenium.common.exceptions.NoSuchElementException: 訊息。無法定位元素:
有沒有人知道?
有誰能幫助我嗎?
uj5u.com熱心網友回復:
你也許可以使用find_element_by_partial_link_text或find_element_by_link_text函式。
嘗試使用這個鏈接來了解如何使用它。鏈接
uj5u.com熱心網友回復:
在Selenium中有4種點擊方式。
我將使用這個xpath
//span[contains(text(), 'about us')]//parent::a[@id='about_page']
代碼試用1 :
time.sleep(5)
driver.find_element_by_xpath("//span[contains(text(), 'about us')]//parent::a[@id='about_page']").click()
代碼試用2 :
WebDriverWait(driver, 20).until(EC. element_to_be_clickable((By.XPATH, "//span[contains(text(), 'about us')]//parent::a[@id='about_page']")).click()
代碼試用3 :
time.sleep(5)
button = driver.find_element_by_xpath("//span[contains(text(), 'about us')]//parent::a[@id='about_page']"/span>)
driver.execute_script("arguments[0].click();", button)
代碼試用4 :
time.sleep(5)
button = driver.find_element_by_xpath("//span[contains(text(), 'about us')]//parent::a[@id='about_page']"/span>)
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/houduan/309764.html
標籤:
上一篇:如何刮取行中有行的網路表格?
