如何读取#shadow-root(用户代理)下的文本(Java-Selenium)

发布于 2025-01-16 09:47:18 字数 1813 浏览 1 评论 0原文

我试图找到属于#shadow-root(用户代理)的文本。我尝试了多种可以在这里找到的解决方案,但没有一个对我有用。尝试使用JavascriptExecutor。但这没有用。 我在 Java 语言中使用 selenium

要求:获取名字和姓氏并打印它。 名字:TEST 和姓氏:USER 位于 #shadow-root(用户代理)

下 下面是 HTML

 <div class="form-group 
    required">
    <label class="form-control-label" for="firstName">
        First Name
    </label>
    <input type="text" class="form-control" id="firstName" data-missing-error="Please enter a value for Name" data-pattern-mismatch="Only letters, numbers, hyphens and apostrophes are allowed in someone's first name" data-range-error="Must be 30 characters or less." name="dwfrm_profile_customer_firstname" required="" aria-required="true" value="TEST" maxlength="30" pattern="^[a-zA-Z0-9-'][A-Za-z0-9-' ]*$">
    #shadow-root (user-agent)
   <div> TEST</div>
   </input>
    <div class="invalid-feedback"></div>
</div>

<div class="form-group
    required">
    <label class="form-control-label" for="lastName">
        Last Name
    </label>
    <input type="text" class="form-control" id="lastName" data-missing-error="Please enter a value for Surname" data-pattern-mismatch="Only letters, numbers, hyphens and apostrophes are allowed in someone's last name" data-range-error="Must be 30 characters or less." name="dwfrm_profile_customer_lastname" required="" aria-required="true" value="USER" maxlength="30" pattern="^[a-zA-Z0-9-'][A-Za-z0-9-' ]*$">
   #shadow-root (user-agent)
    <div> TEST</div>[![enter image description here][1]][1]
    </input>
    <div class="invalid-feedback"></div>
</div>

在此处输入图像描述

I am trying to find a text which falls under #shadow-root (user-agent). I tried multiple solutions I could find here, but none of them worked for me.Tried to use JavascriptExecutor. But it didn't work.
I am using selenium in Java language

Requirement: get the First and Last name and print it.
First Name: TEST and Last Name: USER is coming under #shadow-root (user-agent)

Below is the HTML

 <div class="form-group 
    required">
    <label class="form-control-label" for="firstName">
        First Name
    </label>
    <input type="text" class="form-control" id="firstName" data-missing-error="Please enter a value for Name" data-pattern-mismatch="Only letters, numbers, hyphens and apostrophes are allowed in someone's first name" data-range-error="Must be 30 characters or less." name="dwfrm_profile_customer_firstname" required="" aria-required="true" value="TEST" maxlength="30" pattern="^[a-zA-Z0-9-'][A-Za-z0-9-' ]*
quot;>
    #shadow-root (user-agent)
   <div> TEST</div>
   </input>
    <div class="invalid-feedback"></div>
</div>

<div class="form-group
    required">
    <label class="form-control-label" for="lastName">
        Last Name
    </label>
    <input type="text" class="form-control" id="lastName" data-missing-error="Please enter a value for Surname" data-pattern-mismatch="Only letters, numbers, hyphens and apostrophes are allowed in someone's last name" data-range-error="Must be 30 characters or less." name="dwfrm_profile_customer_lastname" required="" aria-required="true" value="USER" maxlength="30" pattern="^[a-zA-Z0-9-'][A-Za-z0-9-' ]*
quot;>
   #shadow-root (user-agent)
    <div> TEST</div>[![enter image description here][1]][1]
    </input>
    <div class="invalid-feedback"></div>
</div>

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心病无药医 2025-01-23 09:47:18
 WebElement shadowHost = driver.findElement(By.id("firstname"));
 SearchContext shadowRoot = (SearchContext) ((JavascriptExecutor)driver)
.executeScript("return.argument[0].shadowRoot",shadowHost);

 String textFName = shadowRoot.findElement(By.tagName("div")).getText();


 WebElement shadowHostL = driver.findElement(By.id("lastName"));
 SearchContext shadowRootL = (SearchContext) ((JavascriptExecutor)driver)
.executeScript("return.argument[0].shadowRoot",shadowHostL);

 String textFName = shadowRootL.findElement(By.tagName("div")).getText();

Maven依赖添加

<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>4.1.2</version>
</dependency>
 WebElement shadowHost = driver.findElement(By.id("firstname"));
 SearchContext shadowRoot = (SearchContext) ((JavascriptExecutor)driver)
.executeScript("return.argument[0].shadowRoot",shadowHost);

 String textFName = shadowRoot.findElement(By.tagName("div")).getText();


 WebElement shadowHostL = driver.findElement(By.id("lastName"));
 SearchContext shadowRootL = (SearchContext) ((JavascriptExecutor)driver)
.executeScript("return.argument[0].shadowRoot",shadowHostL);

 String textFName = shadowRootL.findElement(By.tagName("div")).getText();

maven dependency to add

<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>4.1.2</version>
</dependency>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文