如何在 chrome 浏览器 selenium webdriver 中使用多个窗口并根据需要切换选项卡 [英] How to use multiple window in chrome browser selenium webdriver and switch the tab if required
问题描述
我想复制一个临时电子邮件并粘贴到其他网站 (twitter) 以创建一个帐户.我已完成复制和粘贴过程,但我们需要验证电子邮件 OTP,因此我必须留在第一个站点
driver.get("https://tempail.com/en/");
我需要获得电子邮件 OTP(如何仅获得 OTP 号码的电子邮件 OTP?)
以下是我的完整代码:
driver.get("https://tempail.com/en/");driver.manage().timeouts().implicitlyWait(300, TimeUnit.SECONDS);WebElement copyButton = driver.findElement(By.xpath("//input[@id='eposta_adres']"));String email = copyButton.getAttribute("value");System.out.println(email);driver.get("https://mobile.twitter.com/");driver.findElement(By.xpath("//div[@dir='auto']///*[contains(text(),'用电话或电子邮件注册')]")).click();driver.manage().timeouts().implicitlyWait(80, TimeUnit.SECONDS);WebDriverWait wait = new WebDriverWait(driver, 30);wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("name"))).sendKeys("abc5643754");wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[text()='使用电子邮件代替']"))).click();wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("email"))).sendKeys(email);选择月份 = new Select(wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(SELECTOR_1"))));Select day = new Select(wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(SELECTOR_2"))));Select year = new Select(wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(SELECTOR_3"))));month.selectByValue("10");day.selectByValue("14");year.selectByValue("1992");for(int i=0; i<2; i++) {线程睡眠(1000);wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@dir='auto']///*[contains(text(),'Next')]"))).click();}wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@dir='auto']///*[contains(text(),'Sign up')]"))).click();
现在我需要从临时电子邮件站点复制 OTP 但如果我重新打开该站点已经关闭我收到新的临时电子邮件,所以我们需要留在第一个 URL(tempemail 站点,直到我们从临时站点)
问题说明:
你在做什么
driver.get("https://tempail.com/en/");
然后在相同的代码中,稍后你正在做
driver.get("https://mobile.twitter.com/");
现在,让我们了解执行此操作时会发生什么.它将加载第一个 URL 和交互之后(您用于临时邮件的代码),并将在同一选项卡中加载 https://mobile.twitter.com/
.>
问题解决方案:
您应该使用 Selenium 打开一个新标签并将第二个 URL 加载到该标签中.还要确保在需要时在选项卡之间切换.
示例代码:
driver.manage().window().maximize();WebDriverWait wait = new WebDriverWait(driver, 30);driver.manage().timeouts().implicitlyWait(300, TimeUnit.SECONDS);driver.get("https://tempail.com/en/");WebElement copyButton = driver.findElement(By.xpath("//input[@id='eposta_adres']"));String email = copyButton.getAttribute("value");System.out.println(email);String newURL = "https://mobile.twitter.com/";JavascriptExecutor js = (JavascriptExecutor)driver;jse.executeScript("window.open('"+newURL+"','_blank');");ArrayListtabs = new ArrayList(driver.getWindowHandles());driver.switchTo().window(tabs.get(1));driver.findElement(By.xpath("//div[@dir='auto']///*[contains(text(),'用电话或电子邮件注册')]")).click();wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("name"))).sendKeys("abc5643754");wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[text()='使用电子邮件代替']"))).click();wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("email"))).sendKeys(email);选择月份 = new Select(wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(SELECTOR_1"))));Select day = new Select(wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(SELECTOR_2"))));Select year = new Select(wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(SELECTOR_3"))));month.selectByValue("10");day.selectByValue("14");year.selectByValue("1992");for(int i=0; i<2; i++) {线程睡眠(1000);wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@dir='auto']///*[contains(text(),'Next')]"))).click();}wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@dir='auto']///*[contains(text(),'Sign up')]"))).click();driver.switchTo().window(tabs.get(0));线程睡眠(2000);String[] otpString = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("a[href] div.baslik"))).getText().split("\\ ");字符串 otp = otpString[0];System.out.println("OTP是" + otp);线程睡眠(2000);driver.switchTo().window(tabs.get(1));wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("verfication_code"))).sendKeys(otp);
I want to copy a temp email and paste in other sites(twitter)to create a account. I have completed a copy and paste process but we need to verify the email OTP so for that I have to stay on 1st site
driver.get("https://tempail.com/en/");
and I need to get the email OTP (How to get the email OTP only the OTP number alone ?)
Below is my entire code :
driver.get("https://tempail.com/en/");
driver.manage().timeouts().implicitlyWait(300, TimeUnit.SECONDS);
WebElement copyButton = driver.findElement(By.xpath("//input[@id='eposta_adres']"));
String email = copyButton.getAttribute("value");
System.out.println(email);
driver.get("https://mobile.twitter.com/");
driver.findElement(By.xpath("//div[@dir='auto']//*[contains(text(),'Sign up with phone or email')]")).click();
driver.manage().timeouts().implicitlyWait(80, TimeUnit.SECONDS);
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("name"))).sendKeys("abc5643754");
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[text()='Use email instead']"))).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("email"))).sendKeys(email);
Select month = new Select(wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("SELECTOR_1"))));
Select day = new Select(wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("SELECTOR_2"))));
Select year = new Select(wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("SELECTOR_3"))));
month.selectByValue("10");
day.selectByValue("14");
year.selectByValue("1992");
for(int i=0; i<2; i++) {
Thread.sleep(1000);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@dir='auto']//*[contains(text(),'Next')]"))).click();
}
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@dir='auto']//*[contains(text(),'Sign up')]"))).click();
Now I need to copy the OTP from the temp email site but the site is already closed if I reopen I get new temp email, so we need stay on the 1st URL(tempemail site until we get and capture the OTP from the temp site)
Problem explanation :
You are doing
driver.get("https://tempail.com/en/");
and then in the same code, later on you are doing
driver.get("https://mobile.twitter.com/");
Now, let's understand what happens when you do this. It will load the first URL and and after interaction (the code that you've for tempail), and it will load the https://mobile.twitter.com/
in the same tab.
Problem Solution :
You should open a new tab using Selenium and load the second URL into that tab. Also make sure to switch between tab as and when needed.
Sample code :
driver.manage().window().maximize();
WebDriverWait wait = new WebDriverWait(driver, 30);
driver.manage().timeouts().implicitlyWait(300, TimeUnit.SECONDS);
driver.get("https://tempail.com/en/");
WebElement copyButton = driver.findElement(By.xpath("//input[@id='eposta_adres']"));
String email = copyButton.getAttribute("value");
System.out.println(email);
String newURL = "https://mobile.twitter.com/";
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.open('"+newURL+"','_blank');");
ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));
driver.findElement(By.xpath("//div[@dir='auto']//*[contains(text(),'Sign up with phone or email')]")).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("name"))).sendKeys("abc5643754");
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[text()='Use email instead']"))).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("email"))).sendKeys(email);
Select month = new Select(wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("SELECTOR_1"))));
Select day = new Select(wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("SELECTOR_2"))));
Select year = new Select(wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("SELECTOR_3"))));
month.selectByValue("10");
day.selectByValue("14");
year.selectByValue("1992");
for(int i=0; i<2; i++) {
Thread.sleep(1000);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@dir='auto']//*[contains(text(),'Next')]"))).click();
}
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@dir='auto']//*[contains(text(),'Sign up')]"))).click();
driver.switchTo().window(tabs.get(0));
Thread.sleep(2000);
String[] otpString = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("a[href] div.baslik"))).getText().split("\\ ");
String otp = otpString[0];
System.out.println("OTP is " + otp);
Thread.sleep(2000);
driver.switchTo().window(tabs.get(1));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("verfication_code"))).sendKeys(otp);
这篇关于如何在 chrome 浏览器 selenium webdriver 中使用多个窗口并根据需要切换选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!