gpt4 book ai didi

java - Selenium 在查找第一个元素的前兄弟元素时永远挂起

转载 作者:行者123 更新时间:2023-12-01 09:04:34 25 4
gpt4 key购买 nike

我试图从同一个 web 元素中获取 和 的前一个元素。但由于该 Web 元素是第一个 Web 元素,因此没有前面的同级元素,因此该进程将永远挂起。当我试图从另一个 webelement 找到 webelements (而不是使用驱动程序)时,隐式等待也不起作用。

@BeforeTest
public void init(){
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(2000, TimeUnit.SECONDS);
driver.get("https://account.box.com/login");
}


@Test
public void testWebElem(){
WebElement source = driver.findElement(By.xpath(".//*[@id='login-form']/div/h2"));
System.out.println(getPositionOfWebElem(source));
}


private int getPositionOfWebElem(WebElement elem){
String xpathExpressionFollowing = "./following-sibling::*";
System.out.println("xPath Expression :: " + xpathExpressionFollowing);
List<WebElement> elemsFollowing = elem.findElements(By.xpath(xpathExpressionFollowing));
printWebelems(elemsFollowing);
String xpathExpression = "./preceding-sibling::*";
System.out.println("xPath Expression :: " + xpathExpression);
List<WebElement> elemsts = elem.findElements(By.xpath(xpathExpression));
printWebelems(elemsts);
return 0;
}

最佳答案

减少你的隐式等待,因为 2000 秒是非常 L-O-N-G 等待!否则 webdriver 将等待 2000 秒来查找前面不存在的元素!顺便说一句,如果你喜欢 4 位整数,请使用 TimeUnit.MILLISECONDS

下面的代码片段对我有用

import java.util.List;
import java.util.concurrent.TimeUnit;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;

public class Test22{

WebDriver driver;

@Before
public void init(){
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://account.box.com/login");
}

@Test
public void testWebElem(){
WebElement source = driver.findElement(By.xpath(".//*[@id='login-form']/div/h2"));
System.out.println(getPositionOfWebElem(source));
}


private int getPositionOfWebElem(WebElement elem){
String xpathExpressionFollowing = "./following-sibling::*";
System.out.println("xPath Expression :: " + xpathExpressionFollowing);
List<WebElement> elemsFollowing = elem.findElements(By.xpath(xpathExpressionFollowing));
System.out.println(elemsFollowing.size());
String xpathExpression = "./preceding-sibling::*";
System.out.println("xPath Expression :: " + xpathExpression);
List<WebElement> elemsts = elem.findElements(By.xpath(xpathExpression));
System.out.println(elemsts.size());
return 0;
}

}

关于java - Selenium 在查找第一个元素的前兄弟元素时永远挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41391256/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com