【问题标题】:Couldn't click on element by using the xpath with selenium in python无法通过在 python 中使用带有 selenium 的 xpath 来单击元素
【发布时间】:2018-12-31 14:05:51
【问题描述】:

我正在尝试使用 selenium 框架来了解有关 Web 浏览器自动化的知识。因此,我决定打造一款奥迪车型……

到目前为止我的代码:

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

chrome_driver = webdriver.Chrome(executable_path=r"chromedriver_win32\chromedriver.exe")
chrome_driver.get("https://www.audi.de/de/brand/de/neuwagen.html")

# choose A3 model
chrome_driver.find_element_by_xpath('//*[@id="list"]/div[1]/ul/li[2]/a[2]').click()

# choose sedan version
WebDriverWait(chrome_driver, 3).until(EC.visibility_of_element_located((By.XPATH, '//*[@id="a3limo"]/div/a'))).click()

# start model configuration
WebDriverWait(chrome_driver, 3).until(EC.visibility_of_element_located((By.XPATH, '/html/body/div[1]/div[2]/div/div[6]/div[2]/div/div[1]/ul/li[1]/a'))).click()

# choose the s-line competition package
WebDriverWait(chrome_driver, 3).until(EC.visibility_of_element_located((By.XPATH, '/html/body/div[1]/div[2]/div/div[7]/div[2]/div[2]/div[3]/div[1]/div/div[1]/div/div[1]/span/span'))).click()

# accept the s-line competition package
WebDriverWait(chrome_driver, 3).until(EC.visibility_of_element_located((By.XPATH, '/html/body/div[5]/div/div/div/div/div/ul[2]/li[2]/a'))).click()

现在,代码在start model configuration 行上已经失败(请参阅网页https://www.audi.de/de/brand/de/neuwagen/a3/a3-limousine-2019.html 上的“配置启动”按钮)。 xPath 必须是正确的,并且元素也应该是可见的,那么我在这里做错了什么?

【问题讨论】:

  • 请发布您遇到的异常
  • 我收到超时异常:raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message:

标签: python selenium selenium-webdriver web-scraping


【解决方案1】:

实际需要的链接只有在向下滚动页面时才可见。

尝试使用这段代码点击链接:

configuration_start = chrome_driver.find_element_by_xpath('//a[@title="Konfiguration starten"]')
chrome_driver.execute_script('arguments[0].scrollIntoView();', configuration_start)    
configuration_start.click()

由于导航面板位置固定,可能会与目标链接重叠,因此您可以在处理链接之前更改导航面板样式:

nav_panel = chrome_driver.find_element_by_xpath('//div[@data-module="main-navigation"]')
driver.execute_script('arguments[0].style.position = "absolute";', nav_panel)

【讨论】:

  • raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: unknown error: Element ... 在点 (152, 39) 不可点击。其他元素会收到点击:
  • 使用上面的代码,它有时但并非总是有效。
  • @PythonNoob ,对我来说效果很好,但是好的,我会再试一次。也尝试在chrome_driver.get("https://www.audi.de/de/brand/de/neuwagen.html")之后添加chrome_driver.maximize_window()
  • 不,也没有用。页面向下滚动到“Modellhistory”“Zubehörfinder”“Audi News”,所以“Konfiguration starten”是不可见的......奇怪。
  • 它可以工作,如果我像这样滚动:chrome_driver.execute_script("window.scrollTo(0, 30)")。但这不是很好......
【解决方案2】:

以下似乎对我有用。我添加了等待元素,并通过 javascript 使用了元素的模拟点击。

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

url = 'https://www.audi.de/de/brand/de/neuwagen.html'
d = webdriver.Chrome()
d.get(url)
WebDriverWait(d,10).until(EC.presence_of_element_located((By.CSS_SELECTOR, '[data-filterval="a3"]'))).click()
d.get(WebDriverWait(d,10).until(EC.presence_of_element_located((By.CSS_SELECTOR, '#a3limo .mf-model-details a'))).get_attribute('href'))
element = WebDriverWait(d,10).until(EC.presence_of_element_located((By.CSS_SELECTOR, '[title="Konfiguration starten"]')))
d.execute_script("arguments[0].click();", element)

【讨论】:

  • 虽然这可能有效,但您应该澄清(因为 OP 是自动化的新手)为什么此代码有效以及为什么在您的浏览器自动化中您实际上根本没有点击
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-05
  • 2021-01-20
  • 2019-04-13
  • 2019-03-19
  • 1970-01-01
  • 2019-10-13
  • 1970-01-01
相关资源
最近更新 更多