如何使用java将webelement转换为selenium中的字符串?请参阅详细信息部分了解更多信息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33029579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert webelement to string in selenium using java? Please refers to detail section for more info
提问by TodayILearned
I created a POM
for 'Create project' page
我POM
为“创建项目”页面创建了一个
public static class addProjInfo_container
{
public static WebElement ProjName_txt(WebDriver driver)
{
element=driver.findElement(By.xpath("//label[text()='Project Name']/following-sibling::input"));
return element;
}
// and so on for every text field for adding project...
And I created a TestUtility
class with method for waitForElement
as show below
我创建了一个TestUtility
带有方法的类,waitForElement
如下所示
public final class TestUtility {
private static WebDriver driver;
public static void waitforElementXpath(final WebDriver driver,final int waitTime,final String xp)
{
WebDriverWait wait =new WebDriverWait(driver, waitTime);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(xp)));
}
}
Now, in Test script I want to avoid using Thread.sleep()
to wait for webelement to be ready to start performing actions.
现在,在测试脚本中,我想避免使用Thread.sleep()
等待 webelement 准备好开始执行操作。
so, I use
所以,我用
TestUtility.waitforElementXpath(driver,10,CreateProject_Page.addProjInfo_container.projName_txt(driver));
But,it displays error as
但是,它显示错误为
The method waitforElementXpath(WebDriver, int, String) in the type TestUtility is not applicable for the arguments (WebDriver, int, WebElement)
Kindly, let me know how to handle the issue.
请让我知道如何处理这个问题。
回答by Alan M
Basically your want to reverse the By to get its string and you are using xpath
基本上你想反转 By 来获取它的字符串并且你正在使用 xpath
so change to this which return the String instead of WebElement
所以更改为返回字符串而不是 WebElement
public static class addProjInfo_container {
public static String projName_txt(WebDriver driver) {
By by = By.xpath("//label[text()='Project Name']/following-sibling::input");
driver.findElement(by);
return getSelectorAsString(by);
}
public static String getSelectorAsString(By by) {
String str = by.toString();
return str.substring(str.indexOf(" ") , str.length());
}
// and so on for every text field for adding project...
}
hope this could help
希望这会有所帮助
回答by JeffC
This is really a convoluted way of trying to accomplish this task. Your ProjName_txt()
method already has found the element because that's what it returns so you don't need to wait for it to appear by using waitforElementXpath()
. I would recommend that you read some articles on OOP and classes before you write too much more code.
这确实是尝试完成此任务的复杂方法。您的ProjName_txt()
方法已经找到了该元素,因为这是它返回的内容,因此您无需使用waitforElementXpath()
. 我建议您在编写更多代码之前阅读一些关于 OOP 和类的文章。
回答by user8156329
The best way is:
最好的办法是:
String name = driver.findElementByClassName("classnamesample").getText() ;
Just add .getText()
in the last of xpath and receive this as String
.
只需添加.getText()
最后一个 xpath 并将其作为String
.