Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.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/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
Java 如何解决Selenium超时异常_Java_Selenium_Selenium Webdriver - Fatal编程技术网

Java 如何解决Selenium超时异常

Java 如何解决Selenium超时异常,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,LoginPage.java public LoginPage(WebDriver driver){ //initialize elements PageFactory.initElements(driver, this); } public void set_email(String useremail){ email.clear(); email.sendKeys(useremail); } public vo

LoginPage.java

   public LoginPage(WebDriver driver){
     //initialize elements
     PageFactory.initElements(driver, this);
   }

   public void set_email(String useremail){
       email.clear();
       email.sendKeys(useremail);
   }

   public void set_password(String userpassword){
        password.clear();
        password.sendKeys(userpassword);
   }

   public void click_button(){
        button.submit();
   }

   public void equal_titles(String actualTitle,String expectedTitle){
        Assert.assertEquals(actualTitle,expectedTitle);
   }

   public void set_reset_email(String resetemail){
        reset_psw_email.clear();
        reset_psw_email.sendKeys(resetemail);
   }

   public void click_reset_button(){
        reset_button.submit();
   }
   @BeforeTest
   public void setup(){
       System.setProperty("webdriver.gecko.driver", "\\GeckoDriver\\geckodriver.exe");
       driver=new FirefoxDriver();
       driver.manage().window().maximize();
       driver.get("https://test.admin.placer.life/login");
   }

   @Test(priority=9)
   public void reset_psw(){
       LoginPage login=new LoginPage(driver);
       driver.findElement(By.xpath("//*[@id='app']/div/div[2]/a")).click();     
       WebDriverWait wait = new WebDriverWait(driver, 5);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='app']/div/div[2]/form/div[3]/div[2]/button[contains(text(),'Send Password Reset Link')]")));
       String expectedTitle = "Placer Admin - Password recovery";
       String actualTitle = driver.getTitle();
       login.equal_titles(actualTitle, expectedTitle);
   }

   @Test(priority=8)
   public void reset_email(){
       LoginPage login=new LoginPage(driver);
       driver.findElement(By.xpath("//*[@id='app']/div/div[2]/a")).click();
       WebDriverWait wait = new WebDriverWait(driver, 10);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='app']/div/div[2]/form/div[3]/div[2]/button[contains(text(),'Send Password Reset Link')]")));
       login.set_reset_email("");
       login.click_reset_button();  
       WebDriverWait wait_reset_psw = new WebDriverWait(driver, 10);
    wait_reset_psw.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='app']/div/div[2]/form/div[2]/span[2]")));
    Assert.assertEquals(driver.findElement(By.xpath("//*[@id='app']/div/div[2]/form/div[2]/span[2]")).getText(),"The email field is required.");
      // driver.navigate().refresh();
   }

   @AfterTest
   public void close(){
       driver.close();
   }
TestingLogin.java

   public LoginPage(WebDriver driver){
     //initialize elements
     PageFactory.initElements(driver, this);
   }

   public void set_email(String useremail){
       email.clear();
       email.sendKeys(useremail);
   }

   public void set_password(String userpassword){
        password.clear();
        password.sendKeys(userpassword);
   }

   public void click_button(){
        button.submit();
   }

   public void equal_titles(String actualTitle,String expectedTitle){
        Assert.assertEquals(actualTitle,expectedTitle);
   }

   public void set_reset_email(String resetemail){
        reset_psw_email.clear();
        reset_psw_email.sendKeys(resetemail);
   }

   public void click_reset_button(){
        reset_button.submit();
   }
   @BeforeTest
   public void setup(){
       System.setProperty("webdriver.gecko.driver", "\\GeckoDriver\\geckodriver.exe");
       driver=new FirefoxDriver();
       driver.manage().window().maximize();
       driver.get("https://test.admin.placer.life/login");
   }

   @Test(priority=9)
   public void reset_psw(){
       LoginPage login=new LoginPage(driver);
       driver.findElement(By.xpath("//*[@id='app']/div/div[2]/a")).click();     
       WebDriverWait wait = new WebDriverWait(driver, 5);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='app']/div/div[2]/form/div[3]/div[2]/button[contains(text(),'Send Password Reset Link')]")));
       String expectedTitle = "Placer Admin - Password recovery";
       String actualTitle = driver.getTitle();
       login.equal_titles(actualTitle, expectedTitle);
   }

   @Test(priority=8)
   public void reset_email(){
       LoginPage login=new LoginPage(driver);
       driver.findElement(By.xpath("//*[@id='app']/div/div[2]/a")).click();
       WebDriverWait wait = new WebDriverWait(driver, 10);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='app']/div/div[2]/form/div[3]/div[2]/button[contains(text(),'Send Password Reset Link')]")));
       login.set_reset_email("");
       login.click_reset_button();  
       WebDriverWait wait_reset_psw = new WebDriverWait(driver, 10);
    wait_reset_psw.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='app']/div/div[2]/form/div[2]/span[2]")));
    Assert.assertEquals(driver.findElement(By.xpath("//*[@id='app']/div/div[2]/form/div[2]/span[2]")).getText(),"The email field is required.");
      // driver.navigate().refresh();
   }

   @AfterTest
   public void close(){
       driver.close();
   }
结果

已通过:重置\u电子邮件

失败:重置\u psw

org.openqa.selenium.TimeoutException:预期条件失败:等待by.xpath://*[@id='app']/div/div[2]/form/div[3]/div[2]/button[contains(text(),'Send Password Reset Link')]定位的元素的可见性(每隔500毫秒尝试5秒)

运行代码时发生此错误。但当我评论其中一个(重置电子邮件或重置psw)时,另一个将成功通过,不会出现任何错误。我能知道这个问题的解决办法吗?此代码需要做哪些修改

请帮忙,
非常感谢你

它看起来像是按命令做的。 您是否尝试增加WebDrive的等待时间

public void reset_psw()
{
LoginPage login=new LoginPage(driver);
driver.findElement(By.xpath("//*[@id='app']/div/div[2]/a")).click();        
WebDriverWait wait = new WebDriverWait(driver, 60); // <-- Here    
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='app']/div/div[2]/form/div[3]/div[2]/button[contains(text(),'Send Password Reset Link')]")));
String expectedTitle = "Placer Admin - Password recovery";
String actualTitle = driver.getTitle();
login.equal_titles(actualTitle, expectedTitle);
}

public void reset_email()
{
   LoginPage login=new LoginPage(driver);
   driver.findElement(By.xpath("//*[@id='app']/div/div[2]/a")).click();
   WebDriverWait wait = new WebDriverWait(driver, 60);
   wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='app']/div/div[2]/form/div[3]/div[2]/button[contains(text(),'Send Password Reset Link')]")));
   login.set_reset_email("");
   login.click_reset_button();  
   WebDriverWait wait_reset_psw = new WebDriverWait(driver, 60);

   wait_reset_psw.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='app']/div/div[2]/form/div[2]/span[2]")));
   Assert.assertEquals(driver.findElement(By.xpath("//*[@id='app']/div/div[2]/form/div[2]/span[2]")).getText(),"The email field is required.");
  // driver.navigate().refresh();
}    
public void reset_psw()
{
LoginPage login=新的LoginPage(驱动程序);
findelelement(By.xpath(“//*[@id='app']/div/div[2]/a”)。单击();
WebDriverWait wait=new WebDriverWait(driver,60);//根据问题和代码试用的结果部分,Testcase
reset_email()
通过,Testcase
reset_psw()
失败,原因如下:

似乎在
@Test
两个步骤中,代码块几乎是相似的。@首先执行Test with(priority=8),在其中调用函数
login。单击_reset_button();
哪个inturn调用
reset_button.submit();
一旦您被重定向到新页面,您将断言文本,电子邮件字段是必需的。


现在,值得一提的是,当您调用
click()
submit()
并将用户重定向到一个新页面时,这些更改会发生。随着DOM树中的更改,一些/所有元素可能会被添加/删除/修改。因此,在下一次
@Test
中,当您使用
元素的可见性()调用ExpectedConditions时
方法,因为前面的元素既没有附加到DOM,也没有在引擎盖下提出,最终导致

请正确设置您的问题的格式。在当前状态下无法阅读。请编辑它。请将WebDriverWait的变量名重命名为“myWait”。wait是一个关键字,您可以看到:wait.untl(条件)。它可能无法解决问题,但可以保护您免受副作用。嗯,也许您可以发布HTML代码?是的,我想等到新页面显示重置按钮。当我尝试在没有此“等待”的情况下运行代码时,会出现一些错误,如无法找到元素请澄清这一点。您是否收到完全相同的错误?(超时…)或在没有超时的情况下无法定位错误?如果您遇到相同的错误,您就错了。我不希望您删除新的WebdriverWait行,我希望您对其进行编辑。将5更改为60,并对重置\u电子邮件测试执行相同操作。我将更新我的答案。是的,在增加超时后我也遇到相同的错误