1

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?

2
  • Please post the exception which you are getting Commented Dec 31, 2018 at 14:34
  • I'm getting the timeout exception: raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message: Commented Dec 31, 2018 at 15:05

2 Answers 2

2

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)
Sign up to request clarification or add additional context in comments.

8 Comments

raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: unknown error: Element <a x-cq-linkchecker="skip" class="nm-pageOpen nm-intro-icon" href="/de/brand/de/neuwagen/a3/a3-limousine-2019/linien-pakete.html" data-link-pattern="/de/brand/de/neuwagen/a3/a3-limousine-2019/linien-pakete" title="Konfiguration starten">...</a> is not clickable at point (152, 39). Other element would receive the click: <label class="nm-navigation-toggle-label "
With the code above, it works only sometimes but not always.
@PythonNoob , Works fine for me, but OK, I'll try to check it again. Also try to add chrome_driver.maximize_window() right after chrome_driver.get("https://www.audi.de/de/brand/de/neuwagen.html")
Nope, didn't work either. The page scrolls down to "Modellhistorie" "Zubehörfinder" "Audi News", so that the "Konfiguration starten" is not visible... strange.
It works, if I scroll like: chrome_driver.execute_script("window.scrollTo(0, 30)"). But thats not great...
|
1

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)

1 Comment

While this might work you should clarify (as OP is newbie to automation) WHY this code works and WHY in your browser-automation you actually do no clicks at all

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.