0

I'm trying to click on Jetzt bewerben available in this webpage using selenium. The script that I've written so far can click on that grid If I stick with hardcoded delay. I would like to do the clicking without using any hardcoded delay.

I've tried with:

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

url = "https://jobs.deloitte.de/job/D%C3%BCsseldorf-Werkstudent-%28mwd%29-Administration-Business-Process-Solutions/522320501/"

driver = webdriver.Chrome()
driver.get(url)
wait = WebDriverWait(driver,10)

time.sleep(5)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,"a#bewerben_top > h1"))).click()

How can I click on that grid shaking off hardcoded delay?

SMTH
  • 67
  • 1
  • 4
  • 17
  • What happens when you don't put the delay? – Tarun Lalwani May 21 '20 at 05:42
  • The scripts doesn't seem to click on that link when I don't put the delay. It doesn't throw any error either. – SMTH May 21 '20 at 06:02
  • Can you check if there are any console errors? So what could be happening that the page gets loaded, the element also gets loaded but either the element is not bounded to a javascript action or relevant javascript is still being loaded. So your `visibility_of_element_located` will see the element but not do anything. Another way could to be wait for some other element which you know would be there once the page is full loaded – Tarun Lalwani May 21 '20 at 06:05

2 Answers2

2

There is not a problem with a fact that button is not clickable or not visible. The problem is that there is Javascript doing some "magic" to the button.

All you need to do is to wait until the document is complete.

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

url = "https://jobs.deloitte.de/job/D%C3%BCsseldorf-Werkstudent-%28mwd%29-Administration-Business-Process-Solutions/522320501/"

driver = webdriver.Chrome()
driver.get(url)
wait = WebDriverWait(driver,10)

wait.until(lambda d: d.execute_script("return document.readyState")=='complete')
driver.find_element_by_css_selector("a#bewerben_top > h1").click()
puchal
  • 1,883
  • 13
  • 25
0

Your locator strategy was perfect. However, there are a couple of things:

  • It seems the Browsing Context of the website https://jobs.deloitte.de/job/D%C3%BCsseldorf-Werkstudent-%28mwd%29-Administration-Business-Process-Solutions/522320501/ returns back the control even before document.readyState turns complete.
  • Ideally, you should acknowledge the Acceptance of the website's cookie policy.
  • As per best practices, while invoking click() you need to induce WebDriverWait for the element_to_be_clickable().
  • You can use the following Locator Strategies:

    driver.get("https://jobs.deloitte.de/job/D%C3%BCsseldorf-Werkstudent-%28mwd%29-Administration-Business-Process-Solutions/522320501/")
    WebDriverWait(driver, 10).until(lambda driver: driver.execute_script('return document.readyState') == 'complete')
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#cookie-acknowledge"))).click()
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a#bewerben_top > h1"))).click()
    
  • Note: You have to add the following imports:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • PS: In such one off scenarios you may require to wait for the jQuery to complete as follows:

    WebDriverWait(driver, 20).until(lambda driver: driver.execute_script('return jQuery.active') == 0)
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352