我将Webdriver中的Boolean
变量定义为:
boolean r1 = selenium.isTextPresent("something");
我在while
循环中使用了它。我想将我的代码转换为Webdriver代码,我尝试了:
boolean jr1 = driver.findElement(By.linkText("something")) != null;
但它仅在文本存在且值为true
时才有效。当值应该false
返回到我在控制台错误中收到的变量时:
Caused by: org.openqa.selenium.remote.ErrorHandler$UnknownServerException: Unable to locate element: {"method":"link text","selector":"something"}
你能给我任何建议吗?
答案 0 :(得分:2)
我通过编写一个返回true或false的isElementPresent方法解决了这个问题。 编辑 - 刚刚在另一个答案中看到了这个确切的方法,我不再相信我写了它。显然我很久以前就发现它已经使用了很长时间,感觉就像我的!全部归功于原作者
private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
然后您可以使用:
boolean jr1 = isElementPresent(By.linkText("Something"));
将此方法添加到基类并从中扩展测试类,或将其添加到每个测试类中。
答案 1 :(得分:0)
如果元素没有linktext'something',Webdriver将始终返回我害怕的异常。
您是否尝试过预期条件?
try {
wait.until(ExpectedConditions.presenceOfElementLocated(By.linkText("something")
Boolean jr1 = True
}
catch (Exception e) {
Boolen jr1 = False
}
这是一个相当粗略的实现,但在这种情况下,我将尝试找到元素,如果它找到它,True。如果请求超时,则为False。
答案 2 :(得分:0)
对我来说,最简单的isTextPresent实现为webdriver代码如下:
public boolean isTextPresent(String text) {
String allText = webDriver.findElement(By.tagName("body")).getText();
boolean isTextPresent = allText.contains(text);
return isTextPresent;
}
答案 3 :(得分:0)
我通过以下方法解决了这个问题:
否则返回false
public boolean isRecordDisplayed(String sDisplayText){
Boolean isNoRecordDisplayedMsg = false;
WebElement search =(new WebDriverWait(webDriver,50).until(ExpectedConditions.elementToBeClickable(table)));
package sample;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import java.util.ArrayList;
import java.util.Optional;
/**
* Created by Harshit on 12-04-2017.
*/
public class vstaffcontroller{
dbcon db = new dbcon();
public MenuBar menubar;
public Menu File,Help,Edit;
public MenuItem logoutmenu,aboutmenu,refresh;
public ObservableList<Person> list = FXCollections.observableArrayList();
public TableView<Person> table = new TableView<Person>();
public TableColumn compcol1;
public TableColumn compcol2;
public void logout(){
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setHeaderText("Are you sure you want to continue");
Optional<ButtonType> result = alert.showAndWait();
if(result.get()==ButtonType.OK){
System.exit(0);
}
else{
alert.close();
}
}
public void about(){
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("About the Software");
alert.setContentText("This window is for Verification Staff. \n Click Refresh for refreshing the complaints \n Other verification and other options are in Edit Menu");
alert.setHeaderText("IP of Antilia Technologies - Harshit Pareek");
alert.showAndWait();
}
public void refresh(){
compcol1.setCellValueFactory(
new PropertyValueFactory<Person,String>("complaint_no")
);
compcol2.setCellValueFactory(
new PropertyValueFactory<Person,String>("details")
);
String statement = "SELECT complaint_no,details FROM complaints";
db.getcompData(statement,list);
System.out.print(list);
table.setItems(list);
}
}
按table = By.xpath(“// div [contains(@ class,'record')] // th”);