Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Selenium 不要等待太久的元素_Selenium_Xpath - Fatal编程技术网

Selenium 不要等待太久的元素

Selenium 不要等待太久的元素,selenium,xpath,Selenium,Xpath,我有一张几排的桌子。其中一些行可能有特定的元素,而其他行可能没有。当然,有些人会,有些人不会 我找到该行并将其放入WebElement中。现在,为了查看元素是否存在,我执行以下操作(假设xp=“.someelement”) 使用更详细的异常。这也可以正常工作,但问题相同。它会等待一分钟或半分钟 有没有一种方法可以更快地检查元素是否存在?如果它是相对于驱动程序(driver.findElementBy())而不是相对于元素(row.findElementBy()),我想我可能知道如何做 这是Jav

我有一张几排的桌子。其中一些行可能有特定的元素,而其他行可能没有。当然,有些人会,有些人不会

我找到该行并将其放入WebElement中。现在,为了查看元素是否存在,我执行以下操作(假设xp=“.someelement”)

使用更详细的异常。这也可以正常工作,但问题相同。它会等待一分钟或半分钟

有没有一种方法可以更快地检查元素是否存在?如果它是相对于驱动程序(driver.findElementBy())而不是相对于元素(row.findElementBy()),我想我可能知道如何做


这是Java。

在您的第一个示例中,您有一个元素列表,您没有试图查找一个元素,而是几个元素(假设是行的集合而不是一行)。第二个元素ele正在查找(或试图查找)特定项(假设是一行)。因此,理想情况下,您应该在评论中指出,ELE中不存在某些元素。然而,时间问题可能归结为隐含或显式等待。请阅读更多信息

我更喜欢第一种方法,即检查元素集合(这样您可以将其瞄准xpath并找到包含的所有标记(或者根本没有)。理想情况下,您应该进行显式等待

下面是等待的方法,它将根据元素在轮询时间内是否存在(例如10秒)返回true/or false;值得注意的是,如果在10秒限制之前发现元素存在,则循环将中断并返回true。当然,您可以使用timeOut来实现所需的结果;但不要使用fast(如2秒),否则您可能会冒测试偶尔失败的风险,因为尚未加载表:

public boolean waitForElement(String elementXpath, int timeOut) {

    try{                    
    WebDriverWait wait = new WebDriverWait(driver, timeOut); 
    boolean elementPresent=wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(elementXpath)).isDisplayed());

    System.out.printf("%nElement is present [T/F]..? ")+elementPresent;
    }        
    catch(TimeoutException e1){e1.printStackTrace();elementPresent=false;}          

    return elementPresent;   
 }
我猜您已经在使用30秒的显式等待来完成所有findElement尝试,因此存在差异

希望以上有帮助


祝你好运!

另一个选择是使用WebDriverWait(显式等待),而不是隐式等待

这基本上使你的测试在你告诉他们的时候只会等待很长时间,否则如果他们没有找到你要找的元素,他们会立即失败

改编自



公共类等待{
显示公共静态布尔值(按定位器、整数…超时){
试一试{
waitFor(预期条件。元素的可视性已定位(定位器),
(timeout.length=0:null?超时[0];
}捕获(TimeoutException异常){
返回false;
}
返回true;
}
//添加要等待的其他方法
//查看ExpectedConditions类的源代码,了解如何创建
//你自己的
//例如,元素的存在
//定位器更改的结果数
//元素完成移动
//url更改等。
私有静态void waitFor(ExpectedCondition,整数超时){
timeout=timeout!=null?timeout[0]:5;//如果没有给定值,则默认超时
WebDriverWait wait=新的WebDriverWait(驱动程序,超时);
等到(条件);
}
}

那么在任何一节课上你都可以 By submitButtonBy=By.cssSelector(“.submit”); 使用WaitUntil.displayed(submitButtonBy); 它将等待5秒。如果要等待10秒: 使用WaitUntil.displayed(submitButtonBy,10)


让一个类包含一系列这样的方法的好处是,可以很容易地添加额外的异常,这样,如果有过时的元素或其他东西,您就可以选择返回false,而不必处理页面类或测试类中的try catch。

查看WebDriverWait如何将row元素传递给wait?您可以吗说new WebDriverWait(someElement,30)?是的,差不多了!只需将该方法与行的xpath一起使用即可。因此,您可以键入:waitForElement(“//div[@class=”'row\u xpath'//td//tr”,10),但与其每次都键入超时,不如将其存储在var(timeout)中只有在你知道你需要等待更多的特殊情况下才改变它。嗨,托尼,如果我帮忙的话,你能帮我投票吗?提前谢谢!)
 try {
    WebElement ele = row.findElement(by.xpath(xp));
 } catch (Exception ex) {
   // element is not there
 }
public boolean waitForElement(String elementXpath, int timeOut) {

    try{                    
    WebDriverWait wait = new WebDriverWait(driver, timeOut); 
    boolean elementPresent=wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(elementXpath)).isDisplayed());

    System.out.printf("%nElement is present [T/F]..? ")+elementPresent;
    }        
    catch(TimeoutException e1){e1.printStackTrace();elementPresent=false;}          

    return elementPresent;   
 }
// Use this class whenever you have to access the driver
// And you should only have to setDriver in a BeforeMethod when setting up.
// This method shows how to do it with a single browser
// This could be converted quite easily to threadlocals for parallel test runs 
public class DriverManager {
private final WebDriver driver;
public static WebDriver getDriver() {
  return driver;
}
public static setDriver(WebDriver driver) {
  DriverManager.driver = driver;
}
public class WaitUntil {
public static Boolean displayed(By locator, Integer... timeout) {
    try {
        waitFor(ExpectedConditions.visibilityOfElementLocated(locator),
        (timeout.length = 0 : null ? timeout[0];    
    } catch (TimeoutException exception) {
        return false;
    }
    return true;
}
// add additional methods you want to wait for
// Look at the source of the ExpectedConditions class to see how to create 
// your own
// e.g. presence of element
// number of results from locator changes
// element done moving
// url changes, etc.


private static void waitFor(ExpectedCondition<WebElement> condition, Integer timeout) {
    timeout = timeout != null ? timeout[0] : 5; //default timeout if no value given
    WebDriverWait wait = new WebDriverWait(driver, timeout);
    wait.until(condition);
}