简体   繁体   English

通过 Python 和 Selenium 在#shadow-root(打开)中单击按钮的最佳方式

[英]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:我的目标:

  1. Open web page http://www.easyCredit.de打开 web 页面http://www.easyCredit.de
  2. Accept the Cockies there接受那里的鸡鸡
  3. Click the "Jetzt berechnen" button单击“Jetzt berechnen”按钮
  4. Click the "Zu den finanziellen Angaben" button点击“Zu den finanziellen Angaben”按钮

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>元素。

杰茨特


Solution解决方案

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.

相关问题 Python Selenium can't find element by xpath within #shadow-root (open) using Selenium and Python - Python Selenium can't find element by xpath within #shadow-root (open) using Selenium and Python 无法使用 Python Selenium 在 shadow-root(打开)中定位元素 - Can't locate elments within shadow-root (open) using Python Selenium 无法使用 Selenium 和 Python 在 #shadow-root(打开)中找到登录元素 - Unable to locate the Sign In element within #shadow-root (open) using Selenium and Python 如何使用 Selenium Python 在#shadow-root(打开)中提取信息? - How to extract info within a #shadow-root (open) using Selenium Python? How to locate the First name field within shadow-root (open) within the website https://www.virustotal.com using Selenium and Python - How to locate the First name field within shadow-root (open) within the website https://www.virustotal.com using Selenium and Python 如何使用 Selenium 在#shadow-root(打开)中切换到子框架 - How to switch to the child frame within #shadow-root (open) using Selenium 无法使用 Python Selenium 从 shadow-root 内的元素中提取文本 - Unable to pull text from elements within shadow-root using Python Selenium Python Selenium - 如何在#shadow-root(打开)下的textarea中定位和添加数据 - Python Selenium - How to locate and add data in textarea which is under #shadow-root (open) python selenium:无法定位元素,shadow-root内的svg-icon按钮 - python selenium: unable to locate element, svg-icon button inside shadow-root 无法使用 Selenium Python 将日期输入到带有 shadow-root(用户代理)的字段 - Cannot input date to field with shadow-root (user-agent) with Selenium Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM