如何用 selenium 单击 span 手风琴 python

How to click a span Accordion with selenium python

我一直在尝试使用 selenium python 来点击 enter image description here

中的 Element1 或 Element2 按钮

使用此代码:

element3=driver.find_elements_by_id("span.jump_1").click

element3=driver.find_element_by_css_selector("#jump_1 > button").click

我已经尝试了几个 selectorsxpath..似乎对我没有任何作用。

Selenium 中有 4 种点击方式。

我将使用这个 xpath

//h3[@id='Element_1']

代码试用 1 :

time.sleep(5)
driver.find_element_by_xpath("//h3[@id='Element_1']").click()

代码试用 2 :

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//h3[@id='Element_1']"))).click()

代码试用 3 :

time.sleep(5)
button = driver.find_element_by_xpath("//h3[@id='Element_1']")
driver.execute_script("arguments[0].click();", button)

代码试用 4 :

time.sleep(5)
button = driver.find_element_by_xpath("//h3[@id='Element_1']")
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

此外,如果 idunique,我建议您使用 id 而不是 xpath

此外,我在您分享的 HTML 中没有看到元素 2。