我如何将从后端获取的数据列表与UI元素进行比较?

huangapple go评论76阅读模式
英文:

How can i compare the list<>of data which i have fetched from backend to Ui elements

问题

怎样在我的Java代码中比较从后端获取的数据列表与通过Selenium Java从UI元素的整个div中获取数据?请建议是否有一种方法,我可以获取UI div中所有元素的文本(text()),并将其放入一个列表,而不使用driver.findElements。

英文:

How can i compare the list<>of data which i have fetched from backend into my java code to the ui elements by fetching the data of entire div in selenium java ? Please suggest if there is a way i can fetch the text() of all elements from ui div and put into a list other than driver.findelements

答案1

得分: 1

您可以使用for循环进行迭代并与您的文本进行检查

以下是Java代码我在这里执行的操作是打开Google浏览器输入TESTING”,然后存储所有结果并检查单词为Testing Types”,最后点击该选项

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.List;

public class GooglesearchTests {

    public static void main(String[] args) throws Exception {
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\User\\Downloads\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();

        driver.manage().window().maximize();
        driver.get("https://www.google.com/");
        driver.findElement(By.name("q")).sendKeys("Testing");
        Thread.sleep(Long.parseLong("1000"));
        List<WebElement> LIST = driver.findElements(By.xpath("//ul[@role='listbox']//li/descendant::div[@class='sbl1']"));
        System.out.println(LIST.size());

        for (int i = 0; i < LIST.size(); i++) {
            if (LIST.get(i).getText().contains("testing types")) {
                LIST.get(i).click();
                break;
            }
        }
    }
}
英文:

You can iterate by for loop and check with your text

following code for java , what I am doing here is open google browser and typing as &quot;TESTING&quot; AND store all results and checking word as &quot;Testing Types&quot; and click that option 


    import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.List;

public class GooglesearchTests {

    public static void main(String[] args) throws Exception {
        System.setProperty(&quot;webdriver.chrome.driver&quot;, &quot;C:\\Users\\User\\Downloads\\chromedriver.exe&quot;);
        WebDriver driver = new ChromeDriver();





        driver.manage().window().maximize();
        driver.get(&quot;https://www.google.com/&quot;);
        driver.findElement(By.name(&quot;q&quot;)).sendKeys(&quot;Testing&quot;);
        Thread.sleep(Long.parseLong(&quot;1000&quot;));
        List&lt;WebElement&gt; LIST = driver.findElements(By.xpath(&quot;//ul[@role=&#39;listbox&#39;]//li/descendant::div[@class=&#39;sbl1&#39;]&quot;));
        System.out.println(LIST.size());


        for (int i = 0; i &lt; LIST.size(); i++)
        {
            //System.out.println(LIST.get(i).getText());
            if (LIST.get(i).getText().contains(&quot;testing types&quot;))
            {
                LIST.get(i).click();
                break;
            }
        }
    }
}

答案2

得分: 0

如在问题中提到,您正在寻找driver.findElements()的替代方法。您可以使用以下 JavaScript 执行器来实现相同的功能。

List<String> lst = new ArrayList<String>();
JavascriptExecutor js = (JavascriptExecutor) driver;
Long length = (Long) js.executeScript("return document.getElementsByClassName('r').length");
for (int i = 0; i < length; i++) {
    lst.add((String) js.executeScript("return document.getElementsByClassName('r')[arguments[0]].innerText", i).toString().trim());
}
英文:

As mentioned in the question you are looking for the alternative to the driver.findElements(). You can use JavaScript Executor as below to achieve the same.

    List&lt;String&gt; lst = new ArrayList&lt;String&gt;();
    JavascriptExecutor js = (JavascriptExecutor)driver;
    Long length = (Long) js.executeScript(&quot;return document.getElementsByClassName(&#39;r&#39;).length&quot;);
    for(int i=0; i&lt;length; i++) {
    	lst.add((String) js.executeScript(&quot;return document.getElementsByClassName(&#39;r&#39;)[arguments[0]].innerText&quot;, i).toString().trim());
    }

答案3

得分: 0

你可以轻松使用Java Stream API 完成这个任务。假设你有如下的页面内容(假设你已将代码保存在 /tmp/texts.html):

&lt;html&gt;
  &lt;head/&gt;
  &lt;body&gt;
    &lt;div id=&quot;root&quot;&gt;
      &lt;div&gt;text 1&lt;/div&gt;
      &lt;div&gt;
        &lt;div&gt;text 2&lt;/div&gt;
        &lt;div&gt;text 3&lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/body&gt;
&lt;/html&gt;

你现在可以这样操作:

public void testList() {
    driver.get(&quot;file:///tmp/texts.html&quot;);
    List&lt;WebElement&gt; webElements = driver
            .findElements(
                    By.xpath(&quot;//div[@id=&#39;root&#39;]//div[not(child::*)]&quot;));
    List&lt;String&gt; texts = webElements
            .stream()
            .map(webElement -&gt; webElement.getText())
            .collect(Collectors.toList());
    System.out.println(texts);
}

这个例子演示了如何使用单一的流表达式从元素中收集文本。

附注 - xpath 中的 [not(child::*)] 部分表示我们只选择叶子节点。

英文:

You can do it easily with Java stream API. Assume you have the page like this (say you have saved the code to /tmp/texts.html):

&lt;html&gt;
  &lt;head/&gt;
  &lt;body&gt;
    &lt;div id=&quot;root&quot;&gt;
      &lt;div&gt;text 1&lt;/div&gt;
      &lt;div&gt;
        &lt;div&gt;text 2&lt;/div&gt;
        &lt;div&gt;text 3&lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/body&gt;
&lt;/html&gt;

You now can do the following:

public void testList() {
    driver.get(&quot;file:///tmp/texts.html&quot;);
    List&lt;WebElement&gt; webElements = driver
            .findElements(
                    By.xpath(&quot;//div[@id=&#39;root&#39;]//div[not(child::*)]&quot;));
    List&lt;String&gt; texts = webElements
            .stream()
            .map(webElement -&gt; webElement.getText())
            .collect(Collectors.toList());
    System.out.println(texts);
}

This example demonstrates how you collect texts from the elements with a single stream expression.

P.S. - this [not(child::*)] part of xpath means we're taking the leaf nodes only.

huangapple
  • 本文由 发表于 2020年9月11日 13:48:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63841432.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定