如何使selenium等到下一页加载相同的URL并具有相同的字段

时间:2017-10-17 08:45:02

标签: selenium testing selenium-webdriver automated-tests wait

我的代码如下所示:

driver.findElement(By.id("submit")).sendKeys(Keys.ENTER); 
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.findElement(By.id("search-trigger")).sendKeys(Keys.ENTER);
driver.findElement(By.id("search-trigger")).sendKeys("Shampoo");
driver.findElement(By.id("search-trigger")).sendKeys(Keys.ENTER);

我想在登录页面之前和之后搜索产品和搜索选项,但是我想在登录页面之后进行搜索 我用过

driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);

但它不起作用,我不能使用

WebElement  “”= driver.findElement(By.id(""));

因为搜索选项位于两个页面上 登录后URL和URL保持不变

1 个答案:

答案 0 :(得分:0)

根据我的理解,简而言之,您希望确保用户在搜索产品之前已登录。但是你将无法使用WebElement“”= driver.findElement(By.id(""));

您是否尝试过明确等待用户名/注销文本...等等

的存在
WebElement element = wait.until(ExpectedConditions.elementIsVisible(By.id(>someid>)));

以下为我工作,尝试在flipkart登录后搜索三星:

public static void f() throws InterruptedException
{       
    By signin = By.xpath(".//*[@id='container']/div/header/div[1]/div[1]/div/ul/li[9]/a");
    Thread.sleep(2000);
    By mobile = By.xpath("html/body/div[2]/div/div/div/div/div[2]/div/form/div[1]/input");
    By password = By.xpath("html/body/div[2]/div/div/div/div/div[2]/div/form/div[2]/input");
    By login = By.xpath("html/body/div[2]/div/div/div/div/div[2]/div/form/div[3]/button");
    By myaccount = By.xpath(".//*[@id='container']/div/header/div[1]/div[1]/div/ul/li[8]/a");
    By search = By.xpath(".//*[@id='container']/div/header/div[1]/div[2]/div/div/div[2]/form/div/div[1]/div/input");    
    //geko driver for firefox
    System.setProperty("webdriver.gecko.driver", "path of geckodriver.exe");
    System.setProperty("webdriver.chrome.driver", "path of chromedriver.exe");
    driver = new ChromeDriver();

    driver.get("https://www.flipkart.com/");
    driver.findElement(signin).click();;
    driver.findElement(mobile).sendKeys("xxxxxxxxx");
    driver.findElement(password).sendKeys("xxxxxxxx");
    driver.findElement(login).click();

    WebDriverWait wait = new WebDriverWait (driver, 10);
    wait.until(ExpectedConditions.elementToBeClickable(myaccount));
    driver.findElement(search).sendKeys("samsung");     
}