[英]Best way to click a button within #shadow-root (open) via Python and Selenium
I am currently dabbling in Python in combination with Selenium.我目前正在与 Selenium 一起涉足 Python。 Here I came across the topic of Java Script buttons.
在这里,我遇到了 Java 脚本按钮的主题。 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.
我正在寻找最聪明的方法来找出如何以最简单的方式执行 JavaScript 代码(在本例中为按钮)。
My goals:我的目标:
Number 1 and 2, I got right with a lot of research. 1号和2号,我做了很多研究。 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.我现在已经尝试过类似于 cockies 的方法,但不幸的是它不起作用。 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:这是我要单击的按钮:
The element Jetzt berechnen is within #shadow-root (open) and is an <input>
element.元素Jetzt berechnen在#shadow-root (open)中并且是一个
<input>
元素。
To click on Jetzt berechnen you can use the following line of code:要单击Jetzt berechnen ,您可以使用以下代码行:
berechnen_button = browser.execute_script("""return document.querySelector('kredit-rechner').shadowRoot.querySelector('input.calculator__submit-button')""")
berechnen_button.click()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.