Best way to click a Java Script Button via Python with Selenium


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

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


# wait

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

# wait

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

The last Part does not work.

This is the Button I want to click:

Greetings and thanks


>Solution :

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


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')""")

Leave a ReplyCancel reply