英文:
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
我可能会依赖于不同的因素:
- 互联网的速度
- 您正在访问的服务器速度
- 您使用的计算机速度
- 诸如代理、VPN 等的网络阻碍。
英文:
I may depend on different factors:
- speed of the internet
- the speed of the server you are accessing
- the speed of the machine you are using
- 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论