我一直試圖使用Selenium python來點擊在此輸入圖片描述
的Element1或Element2按鈕。用這個代碼:
element3=driver.find_elements_by_id("span.jump_1"/span>).click
element3=driver.find_element_by_css_selector("#jump_1 > button").click
我已經嘗試了幾個選擇器,運算式...似乎沒有什么對我有用。
uj5u.com熱心網友回復:
在Selenium中,有4種方法可以點擊。
我將使用這個xpath
/h3[@id='Element_1']
代碼試用1 :
time.sleep(5)
driver.find_element_by_xpath("//h3[@id='Element_1']"/span>).click()
代碼試用2 :
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//h3[@id='Element_1']"/span>)) .click()
代碼試用3 :
time.sleep(5)
button = driver.find_element_by_xpath("//h3[@id='Element_1']"/span>)
driver.execute_script("arguments[0].click();", button)
代碼試用4 :
time.sleep(5)
button = driver.find_element_by_xpath("//h3[@id='Element_1']"/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
另外,如果id是unique,我建議你使用id而不是xpath。
另外,我沒有看到你分享的HTML中的元素2。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/309751.html
標籤:
