为什么相同的 Selenium 代码有时候能工作,有时候不能工作?

huangapple go评论78阅读模式
英文:

Why same selenium code sometime work sometime don't?

问题

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
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");
    }
}
英文:

I am using the Selenium webdriver in java. When I run the code, it sometimes works as I want and sometimes error occurs. I have steady internet speed. What is the reason?

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");
}
}

答案1

得分: 0

我可能会依赖于不同的因素:

  1. 互联网的速度
  2. 您正在访问的服务器速度
  3. 您使用的计算机速度
  4. 诸如代理、VPN 等的网络阻碍。
英文:

I may depend on different factors:

  1. speed of the internet
  2. the speed of the server you are accessing
  3. the speed of the machine you are using
  4. network hindrances like proxies, vpn etc.

答案2

得分: 0

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

WebDriver 中有两种类型的等待可用。

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

通过链接获取更多详细信息: Selenium 等待

英文:

Could be a synchronization issue. You Can avoid such issues using waits.

There are two types of waits available in WebDriver.

  • Implicit wait: Its waiting time between each consecutive test step across the entire test script.
  • Explicit wait is used to wait until a particular condition is met or the maximum time has elapsed.

More details available through the link: Selenium Waits

huangapple
  • 本文由 发表于 2020年8月24日 13:47:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/63555353.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定