【发布时间】:2022-06-28 21:27:48
【问题描述】:
Selenium 新手。一直在做一些活动,但时不时地卡住。 另外,如果有任何错误,有时它不会向我显示错误。以下是我的查询。
地址簿是一个存储地址的页面。
网址:http://webapps.tekstac.com/AddressBook/
这是程序: 使用 DriverSetup() 中定义的 getWebDriver() 方法调用驱动程序。 导航到“http://webapps.tekstac.com/AddressBook/”。 识别“昵称”标签文本的祖先。那是表单的祖先“div”。获取此祖先的文本并将其存储在静态变量 fName 中。
这是我 66.67% 评估的代码。
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import java.util.concurrent.TimeUnit;
public class NameLocator //DO NOT Change the class name
{
//Use the declared variables for stroing required values
static String fName;
static String baseUrl;
static WebDriver driver;
public WebDriver setupDriver() //DO NOT Change the method Signature
{
DriverSetup ds = new DriverSetup();
return ds.getWebDriver();
/* Replace this comment by the code statement to create and return the driver */
/* Naviaget to the url 'http://webapps.tekstac.com/AddressBook/' */
}
public String getNameLocator() //DO NOT Change the method Signature
{
WebElement element = driver.findElement(By.xpath("//div[.//[text()='NickName']]/ancestor::div"));
fName = element.getText();
driver.close();
return fName;
/*Using the driver, Find the element ancestor and its text and assign the text to 'fName' */
/*Close the driver*/
}
public static void main(String[] args)
{
NameLocator namLocator=new NameLocator();
//Add required code here
}
}
编译时出错:祖先的 div 文本不正确。
【问题讨论】:
-
具有 text='NickName' 的元素的祖先实际上是
div和id='t1'。并且可以通过css选择器'#t1'简单的找到。然后你可以得到那个元素的文本。这就是你想要的吗? -
@Andrey 我需要找到元素祖先及其文本并将文本分配给“fName”。编译时出现错误 - “祖先的 div 文本不正确”。我不确定我应该在哪里进行更正。
标签: selenium selenium-webdriver xpath ancestor qwebelement