在 Selenium 中基本上有 4 种点击方式。
我将使用这个 xpath
//rect[@class='highcharts-point highcharts-color-0 highcharts-point-hover'][@stroke='#ffffff']
代码试用 1:
time.sleep(5)
driver.find_element_by_xpath("//rect[@class='highcharts-point highcharts-color-0 highcharts-point-hover'][@stroke='#ffffff']").click()
代码试用 2:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//rect[@class='highcharts-point highcharts-color-0 highcharts-point-hover'][@stroke='#ffffff']"))).click()
代码试用 3:
time.sleep(5)
button = driver.find_element_by_xpath("//rect[@class='highcharts-point highcharts-color-0 highcharts-point-hover'][@stroke='#ffffff']")
driver.execute_script("arguments[0].click();", button)
代码试用 4:
time.sleep(5)
button = driver.find_element_by_xpath("//rect[@class='highcharts-point highcharts-color-0 highcharts-point-hover'][@stroke='#ffffff']")
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
PS:如果我们在HTML DOM 中有唯一条目,请检查dev tools(谷歌浏览器)。
检查步骤:
Press F12 in Chrome -> 转到element 部分 -> 做一个CTRL + F -> 然后粘贴xpath 看看,如果你想要的element 用@ 得到高亮 987654334@匹配节点。