3

I am currently dabbling in Python in combination with Selenium. Here I came across the topic of Java Script buttons. I am looking for the smartest way to find out how to execute a Java-Script code (in this case a button) the easiest way.

My goals:

  1. Open web page http://www.easyCredit.de
  2. Accept the Cockies there
  3. Click the "Jetzt berechnen" button
  4. Click the "Zu den finanziellen Angaben" button

Number 1 and 2, I got right with a lot of research. Now it's time to click the button. This can be identified quite well in the code, but still I do not succeed in clicking.

<input type="submit" class="calculator__submit-button" value="calculate now">

I have now tried it similar to the cockies, but unfortunately it does not work. I hope you can help me.

Here is my code:

from selenium import webdriver
from selenium.webdriver.common.by import By
import time

def expand_shadow_element(element):
  shadow_root = browser.execute_script('return arguments[0].shadowRoot', element)
  return shadow_root

# link to Chromedriver
browser = webdriver.Chrome('/usr/local/bin/chromedriver')

# maximize window
browser.maximize_window()

# URL
browser.get('https://www.easycredit.de/')

# wait
time.sleep(5)

# accept cookies
coockie_button = browser.execute_script("""return document.querySelector('#usercentrics-root').shadowRoot.querySelector("button[data-testid='uc-accept-all-button']")""")
coockie_button.click()

# wait
time.sleep(5)

# click "Jetzt berechnen" 
berechnen_button = browser.execute_script("""return document.querySelector('rr-62f0ff69f069c').shadowRoot.querySelector("button[class='calculator__submit-button']")""")
berechnen_button.click()

The last part does not work.

This is the button I want to click:

enter image description here

4
  • Which is that Zu den finanziellen Angaben button? Commented Aug 13, 2022 at 19:16
  • My next goal is the "Jetzt berechnen" Button. I added a Screenshot. Commented Aug 13, 2022 at 19:19
  • Can you give me a hint on how to click the button on the next page (Zu den finanziellen Angaben)? There it somehow does not work with this method. Commented Aug 15, 2022 at 19:56
  • Feel free to raise a new question with your new requirements. Commented Aug 15, 2022 at 19:58

2 Answers 2

5

Selenium 4 introduced a new way to access shadow DOM elements:
shadow_root=elem.shadow_root

shadow_root = driver.find_element(By.CSS_SELECTOR, "kredit-rechner").shadow_root
berechnen_button = shadow_root.find_element(By.CSS_SELECTOR, "input.calculator__submit-button")
berechnen_button.click()

Safe solution:

shadow_root = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "kredit-rechner"))).shadow_root
WebDriverWait(shadow_root, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".input.calculator__submit-button"))).click()
Sign up to request clarification or add additional context in comments.

2 Comments

I must be doing something wrong. elem.shadow_root always raises NoSuchShadowRootException for me, on every element (I tried iterating over every element to see if any did not raise, and all raised), even when I see a shadow root on an element using dev tools. I haven't been able to find documentation. I suppose I could read source code and/or file a bug report...
I think my problem is that I misunderstood shadow roots in general. It's not clear in any documentation I've found, but some people claim that you can't use JavaScript to access a shadow root created by a browser and I guess that applies to Selenium too, which is not surprising I guess.
1

The element Jetzt berechnen is within #shadow-root (open) and is an <input> element.

Jetzt


Solution

To click on Jetzt berechnen you can use the following line of code:

berechnen_button = browser.execute_script("""return document.querySelector('kredit-rechner').shadowRoot.querySelector('input.calculator__submit-button')""")
berechnen_button.click()

5 Comments

Thank You. Did you test the code? I get "JavascriptException: javascript error: Invalid or unexpected token (Session info: chrome=104.0.5112.79)"
My bad, there was a typo, fixed it. Let me know the results.
Thank You. So I can go for the next Button to try :)
@edstrinova yeap, ofcoarse :)
Can you give me a hint on how to click the button on the next page? There it somehow does not work with this method.

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.