当前位置: 首页 > 知识库问答 >
问题:

Python+Selenium--无法定位图像元素(无法单击图像)

陶宏浚
2023-03-14

顺便说一句,我使用Python 3.8、Chrome 81.0.4044.122和最新版本的Selenium。

到目前为止,我使用:driver=webdriver.chrome()driver.find_element_by_xpath(“path_of_element”).click()单击button,然后找到一个具有图像而不是文本的登录按钮。我尝试了XPath、CSS选择器、id、名称、文本链接、ActionChains(move_to)什么都不起作用。

下面是HTML:请点击我。

如何找到图像按钮的正确XPath并单击它?

driver.find_element_by_css_selector('.nice-select').click()driver.find_element_by_xpath(“/html/body/div1/div[3]/div/div/div/div/div/div/div/div/div/div/div/div/div/div/div/div/div/div/div/div/div/span”).click()driver.find_element_by_xpath(“v/div/div/div/div/div/div/div2/div2/div/ul/li[19]')。单击()driver.find_element_by_xpath('/html/body/div1/div[3]/div/div/div/div/div/div/div/div/div/div/div/div/div/div/div/div/div/div/div/div/li[3]')。单击()driver.find_element_by_xpath('/html/body/div/div/div/li[3]')html/body/div1/div[3]/div/div/div/div/div/div/div/div2/div[4]/div/span')。单击()driver.find_element_by_xpath(”/html/body/div1/div[3]/div/div/div/div/div/div/div/div[5]/a')。单击()driver.find_element_by_xpath(“/html/body/div1/div[3]/div/div/div/div/div/div[5]/a')

共有1个答案

汪耀
2023-03-14

尝试以下CSS选择器:

.my_menu>a

代码应该如下所示:

driver.find_element_by_css_selector(".my_menu>a").click()

另外,尝试定位具有显式等待的元素:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By


WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".my_menu>a"))).click()
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import time


driver = webdriver.Chrome()
driver.get("https://oc31.ebssw.kr/onlineClass/search/onlineClassSearchView.do?schulCcode=00898&schCssTyp=online_mid")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable(
    (By.CSS_SELECTOR, ".my_menu>a"))).click()
time.sleep(5)
 类似资料: