如何更快地从Selenium WebElement获取值? [英] How can I get values from Selenium WebElement more than faster?
问题描述
这是我从Selenium WebElement获得价值的测试代码.
This is my test code to get value from Selenium WebElement.
import java.util.List;
import org.apache.commons.lang3.ObjectUtils.Null;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Scan extends WebDriverException {
private long start = 0;
private WebDriver driver = null;
public static void main(String[] args) {
Scan scan = new Scan();
scan.driver = new FirefoxDriver();
scan.driver.get("https://en.wikipedia.org/");
scan.scanAllElements();
scan.driver.quit();
}
public void scanAllElements() {
// get all elements
List<WebElement> elms = driver.findElements(By.xpath("//*"));
System.out.println("elms size:" + elms.size());
// start timer
this.start = System.currentTimeMillis();
// scan all elements and get some value.
for (WebElement elm : elms) {
elm.getTagName();
elm.getAttribute("class");
elm.getAttribute("id");
elm.getAttribute("href");
elm.getText();
elm.getSize();
elm.getLocation();
}
// check the time
stopTimer(elms.size());
}
public void stopTimer(int elmsSize) {
long end = System.currentTimeMillis();
long ms = end - this.start;
long sec = ms / 1000;
long min = sec / 60;
System.out.println("--- Speed Test ---");
System.out.println(ms + " ms");
System.out.println(sec + " s");
System.out.println(min + " min " + (sec % 60) + " s ");
System.out.println("1 loop average time:" + (ms / elmsSize) + " ms");
}
}
结果是这样.这需要很长时间.我想快点.
The result is this.It takes long time. I'd like to make it fast.
elms size:1031
--- Speed Test ---
123468 ms
123 s
2 min 3 s
each loop average time:119 ms
我做了什么.
1.跳过一些元素
如果值不是我想要的.跳过以(继续)获得另一个获取值.
What I did.
1.Skip some elements
If the value is not that what I want. It's skip to get another getting values with (continue).
此示例获取所有元素(//*).因此,当我得到它时,我过滤了元素,这是个好方法.但是我仍然有几百个元素,我需要最小化处理时间.
This sample get all elemnts (//*). So I filtered elements when I get it which was good way. But I have still some hundred elements and I need minimise the process time.
我测试了Runnable Callable Stream.
I tested Runnable Callable Stream.
可运行和可调用解决了速度问题.处理时间变为40%左右.但是许多元素变成了空!!
Runnable and Callable solved speed problem. The Process time became to around 40%. But many elements became to null !!
流仅将时间最小化10%,并且某些元素为空.
Stream minimise only 10% time and it's also some elements are null.
如果您有任何想法要更快地获得它,请告诉我!
If you have any idea to get it more than faster, please tell me!!
推荐答案
您可以使用Javascript,下面的代码几乎立即返回包含标签名称,id,href,类键的Map的ArrayList:
You can using Javascript, code below will return an ArrayList of Map with tag name, id, href, class keys almost instantly:
ArrayList<Maps> list = (ArrayList) ((JavascriptExecutor) driver).executeScript("return [...document.querySelectorAll(\"*\")].map(e=>{return {tagName:(e.tagName==undefined?null:e.tagName),class:(e.className==undefined?null:e.className),id:(e.id==undefined?null:e.id),href:(e.href==undefined?null:e.href)}})");
您所需要做的就是添加js代码以获取位置和大小.对于文本,您可以使用textContent.
在执行脚本之前,请确保已加载页面.
All you need is to add js code to get location and size. For text you can use textContent.
Before execute the script be sure page is loaded.
这篇关于如何更快地从Selenium WebElement获取值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!