代码之家  ›  专栏  ›  技术社区  ›  PythonNoob

无法在python中使用xpath和selenium来单击元素

  •  1
  • PythonNoob  · 技术社区  · 7 年前

    我正在尝试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 (参见网页上的“Konfiguration Starten”按钮 https://www.audi.de/de/brand/de/neuwagen/a3/a3-limousine-2019.html )xpath必须是正确的,元素也应该是可见的,那么我在这里做了什么错误呢?

    2 回复  |  直到 7 年前
        1
  •  1
  •   QHarr    7 年前

    以下内容似乎对我有用。我添加了waitiesforelements,并通过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)
    
        2
  •  2
  •   Andersson    7 年前

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

    尝试使用此代码单击链接:

    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)
    
    推荐文章