硒没有移动到下一个网页



在我的代码中,当我们单击一个按钮时,将打开一个新的选项卡,然后我们必须选择该选项。然而,selenium在新的选项卡上不起作用,我使用了switch-to-option,但没有选项。我附上下面的代码。请检查

公共类跟踪{

public static void main(String[] args) throws InterruptedException { 

System.setProperty("webdriver.chrome.driver","C:\Users\Toshiba\Desktop\selenium\chromedriver_win32 (2)\chromedriver.exe");
ChromeDriver driver=new ChromeDriver();
driver.get("https://orgd8994c71.crm8.dynamics.com/main.aspx?appid=e474bdc4-6835-ed11-9db1-002248d5d2d5&pagetype=entityrecord&etn=opportunity&id=818c1b86-0565-4021-9b82-cd67fac340a9");
Thread.sleep(3000);
//maximize window screen

driver.manage().window().maximize();
//user id
WebElement username = driver.findElement(By.xpath("//input[@type='email']"));
username.sendKeys("gokul87141@gmail.com");
//user id button
WebElement next = driver.findElement(By.xpath("//input[@type='submit']")); next.click();
Thread.sleep(2000);
//password
WebElement password = driver.findElement(By.xpath("//input[@id='i0118']"));
password.sendKeys("Youaregreat!@#");
Thread.sleep(2000);
//password button
WebElement next1 = driver.findElement(By.xpath("//input[@type='submit']")); next1.click();
Thread.sleep(2000);
//next button
WebElement next2 = driver.findElement(By.xpath("//input[@id='idSIButton9']")); next2.click();
Thread.sleep(22000);
//click button
WebElement next21 = driver.findElement(By.xpath("//*[@id="opportunity|NoRelationship|Form|new.opportunity.Command0.Command10-button"]/span/span[2]")); next21.click();
Thread.sleep(6000);
//switch to option
ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs.get(tabs.size()-1));
Thread.sleep(4000);

//second click button
WebElement next22 = driver.findElement(By.xpath("//button[@type='button']")); next22.click();
Thread.sleep(6000);
driver.switchTo().window(tabs.get(tabs.size()-1));       
}
}

@gokulkrishna

首先——不确定;但如果你的电子邮件和密码在你的问题中是合法的,你应该尽快删除它们!

我看到的是,在你点击之前,你已经得到了窗口句柄。由于您在收集窗口句柄后进行"单击",因此只有1个可用。您所在的当前页面。

一旦你把"点击"放在之前,它应该可以正常工作

ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());

Thann选项卡将获得所有可用的句柄,您可以进行切换。

最新更新