如何执行X路径循环?
for (int i=1;i<3;i++)
String xPath = "//*[@id='rso']//h3/a["+i+"]"
all_elements_text.add(driver.findElement(By.xpath(xPath)).getText());
return all_elements_text.toArray() ; }
我想跳过a[2]
答案 0 :(得分:0)
尝试以下代码
for (int i=1;i<3;i++){
if(i!=2){
all_elements_text.add(driver.find Element(By.xpath("//*[@id='rso']//h3/a["+i+"]")).getText());
return all_elements_text.toArray() ;
}
答案 1 :(得分:0)
使用以下代码实现您所需的相同
public static ArrayList<String> getAllValues()
{
ArrayList<String> all_elements_text = new ArrayList<>();
for(int i=1;i<3;i++)
{
if(i!=2)
{
all_elements_text.add(driver.findElement(By.xpath("//*[@id='rso']//h3/a["+i+"]")).getText());
}
}
return all_elements_text;
}
并从您的测试中调用此方法,以获取所有文本并打印所有值,例如
@Test
public void yourTestCase()
{
for(String s :getAllValues())
{
System.out.println(s);
}
}
答案 2 :(得分:-1)
关于解决方案的几个事实:
Google Analytics
变为脆弱。Documentation
何时更改搜索结果的索引。URL
等。所以记住这些,我们假设我们会:
https://www.google.com/
selenium webdriver
Documentation
selenium webdriver
以下是搜索文字 Documentation
的示例代码,并删除包含 List <WebElement> my_list = driver.findElements(By.xpath("//div[@id='rso']//div[@class='srg']/div[@class='g']//h3/a"));
List <String> my_text = new ArrayList<String>();
for (int i=0; i<my_list.size(); i++)
if(!my_list.get(i).getAttribute("innerHTML").contains("Documentation"))
my_text.add(my_list.get(i).getAttribute("innerHTML"));
一词的结果:
import java.awt.Point;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class StreamMaxFind {
public final static void main(String[] args) {
HashMap<String, Point> points = new HashMap<>();
points.put("p1", new Point(10,20));
points.put("p2", new Point(20,30));
points.put("p3", new Point(2,6));
points.put("p4", new Point(3,16));
points.put("p5", new Point(-20,-30));
Map<Double, List<Map.Entry<String, Point>>> groups = points.entrySet().stream()
.collect(Collectors.groupingBy(elem -> Double.valueOf(getLen(elem))));
Map.Entry<Double, List<Map.Entry<String, Point>>> maxVals = groups.entrySet().stream()
.max((elem1, elem2) -> {
return elem1.getKey().compareTo(elem2.getKey());
})
.get();
System.out.println("max val: " + maxVals.getKey());
maxVals.getValue().stream()
.forEach(point -> {
System.out.println("Key of point: " + point.getKey());
System.out.println("max val x " + point.getValue().getX());
System.out.println("max val y " + point.getValue().getY());
});
}
private static double getLen(Map.Entry<String, Point> point1) {
double x1 = point1.getValue().getX();
double y1 = point1.getValue().getY();
return Math.sqrt(x1*x1 + y1*y1);
}
}