PythonNoob
PythonNoob

Reputation: 1052

Couldn't click on element by using the xpath with selenium in python

I'm trying out the selenium framework to learn something about the web browser automation. Therefore, I decided to build an Audi model...

My code so far:

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()

Now, the code fail already on the line start model configuration (see "Konfiguration starten" button on webpage https://www.audi.de/de/brand/de/neuwagen/a3/a3-limousine-2019.html). The xPath must be correct and the element should also be visible, so what am I doing wrong here?

Upvotes: 1

Views: 270

Answers (2)

QHarr
QHarr

Reputation: 84465

The following seems to work for me. I have added waits for elements and used a simulated click of the element via 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)

Upvotes: 1

Andersson
Andersson

Reputation: 52685

Actually required link becomes visible only if to scroll page down.

Try to use this piece of code to click link:

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

As navigation panel has fixed position it might overlap target link, so you can change navigation panel style before handling link:

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

Upvotes: 2

Related Questions