英文:
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 "TESTING" AND store all results and checking word as "Testing Types" 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("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++)
{
//System.out.println(LIST.get(i).getText());
if (LIST.get(i).getText().contains("testing types"))
{
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<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());
}
答案3
得分: 0
你可以轻松使用Java Stream API 完成这个任务。假设你有如下的页面内容(假设你已将代码保存在 /tmp/texts.html
):
<html>
<head/>
<body>
<div id="root">
<div>text 1</div>
<div>
<div>text 2</div>
<div>text 3</div>
</div>
</div>
</body>
</html>
你现在可以这样操作:
public void testList() {
driver.get("file:///tmp/texts.html");
List<WebElement> webElements = driver
.findElements(
By.xpath("//div[@id='root']//div[not(child::*)]"));
List<String> texts = webElements
.stream()
.map(webElement -> 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
):
<html>
<head/>
<body>
<div id="root">
<div>text 1</div>
<div>
<div>text 2</div>
<div>text 3</div>
</div>
</div>
</body>
</html>
You now can do the following:
public void testList() {
driver.get("file:///tmp/texts.html");
List<WebElement> webElements = driver
.findElements(
By.xpath("//div[@id='root']//div[not(child::*)]"));
List<String> texts = webElements
.stream()
.map(webElement -> 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论