需要识别提到的Web元素的祖先
新的硒。一直在做一些活动,但时不时地陷入困境。 另外,如果有任何错误,有时不会向我显示错误。以下是我的查询。
地址簿是存储地址的页面。
url: 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文本不正确。
这就是我开始填充代码之前原始模板的样子。这就是他们设计活动的方式,评论不是我手动输入的。
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 WebDriver driver;
public WebDriver setupDriver() //DO NOT Change the method Signature
{
/* 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
{
/*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
}
}
New to Selenium. Been doing some activity but getting stuck every now and then.
Plus if there is any error, at times it is not showing me the error. Below is my query.
Address Book is a page that stores address.
URL: http://webapps.tekstac.com/AddressBook/
This is the procedure:
Invoke the driver using getWebDriver() method defined in DriverSetup().
Navigate to "http://webapps.tekstac.com/AddressBook/".
Identify ancestor of the 'Nick Name' label text. That is the ancestor 'div' of the form. Get the text of this ancestor and store it in a static variable fName.
This is the code for which I am 66.67% evaluated.
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
}
}
Error while compiling: Ancestor's div text is not correct.
THIS IS HOW THE ORIGINAL TEMPLATE LOOKED BEFORE I STARTED FILLING UP THE CODE. THIS IS HOW THEY HAVE DESIGNED THE ACTIVITY AND THE COMMENTS WERE NOT MANUALLY ENTERED BY ME.
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 WebDriver driver;
public WebDriver setupDriver() //DO NOT Change the method Signature
{
/* 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
{
/*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
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据评论代码。屏幕截图带有结果 image link :
Code according to the comments. Screenshot with result is here image link: