【问题标题】:Why same selenium code sometime work sometime don't? [closed]为什么相同的硒代码有时工作有时不工作? [关闭]
【发布时间】:2020-12-12 18:11:48
【问题描述】:

我在 java 中使用 Selenium webdriver。当我运行代码时,它有时会按我的意愿工作,有时会出现错误。我有稳定的网速。是什么原因?

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ByIdOrName;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
public class AytomationPractiseRegistration {

    public static void main(String[] args) throws InterruptedException {
        System.setProperty("webdriver.chrome.driver","C:\\Selenium jar\\chromedriver.exe");
        WebDriver driver=new ChromeDriver();
        driver.get("http://automationpractice.com/");
        By locator=By.className("login");
        WebElement element=driver.findElement(locator);
        element.click();
        WebDriverWait wait = new WebDriverWait(driver, 5);
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("email_create")));
        driver.findElement(By.id("email_create")).sendKeys("debasishgupta2015@gmail.com");
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("SubmitCreate")));
        driver.findElement(By.id("SubmitCreate")).click();
        Thread.sleep(5000);
        String pageName=driver.findElement(By.className("page-heading")).getText();
        System.out.println("Page Heading:"+pageName);
        Thread.sleep(5000);
        //wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("id_state")));
        WebElement elementState=driver.findElement(By.name("id_state"));
        Select selectState=new Select(elementState);
        selectState.selectByVisibleText("Georgia");
    }

}

【问题讨论】:

  • 您的问题不清楚。请提供代码和进一步说明
  • 没有一个电话会告诉你它为什么不工作的代码。
  • 如果您尝试访问的元素是有条件地呈现的,您可能会遇到问题。你应该先检查元素是否存在!
  • 检查是否需要显式等待,这主要是原因。检查您的对象定位器是否不明确。
  • 我编辑了,请检查;)

标签: java selenium selenium-webdriver selenium-chromedriver


【解决方案1】:

我可能取决于不同的因素:

  1. 互联网速度
  2. 您正在访问的服务器的速度
  3. 您正在使用的机器的速度
  4. 代理、vpn 等网络障碍

【讨论】:

    【解决方案2】:

    可能是同步问题。您可以使用等待来避免此类问题。

    WebDriver 中有两种等待类型。

    • 隐式等待:在整个测试脚本中每个连续测试步骤之间的等待时间。
    • 显式等待用于等待满足特定条件或经过最长时间。

    更多详情请点击链接:Selenium Waits

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-01
      • 2022-01-25
      • 1970-01-01
      • 2021-10-31
      • 2014-03-28
      • 2022-10-02
      相关资源
      最近更新 更多