无法向下滚动弹出消息 - Gmail登录

时间:2017-03-07 03:02:14

标签: selenium selenium-webdriver

我正在尝试自动登录gmail页面。一旦我输入所有必需的详细信息,如用户名,通行证,DOB,电子邮件等,然后单击下一步按钮,我会收到一条弹出消息,需要向下滚动直到结束,然后启用接受按钮。

我无法向下滚动弹出页面。以下是我的代码

//import org.apache.bcel.generic.Select;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import com.gargoylesoftware.htmlunit.javascript.host.Window;

public class GmailSignIN {

    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub
WebDriver driver = new FirefoxDriver();

//maximize browser
driver.manage().window().maximize();
driver.get("https://accounts.google.com/SignUp?service=mail&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&ltmpl=default");
driver.findElement(By.xpath(".//*[@id='FirstName']")).sendKeys("Krishna");
driver.findElement(By.xpath(".//*[@id='LastName']")).sendKeys("Krishna");
driver.findElement(By.xpath(".//*[@id='GmailAddress']")).sendKeys("Krishna.Krishna1154");
driver.findElement(By.xpath(".//*[@id='Passwd']")).sendKeys("04Mar1989@");
driver.findElement(By.xpath(".//*[@id='PasswdAgain']")).sendKeys("04Mar1989@");


//Input the month
List<WebElement> month_dropdown = driver.findElements(By.xpath(".//*[@id='BirthMonth']/div"));
//iterate the list and get the expected month
Thread.sleep(3000);
for (WebElement month_ele:month_dropdown){
String expected_month = month_ele.getAttribute("innerHTML");
// Break the loop if match found

Thread.sleep(3000);
if(expected_month.equalsIgnoreCase("August")){
    month_ele.click();
    break;
}

driver.findElement(By.id("BirthMonth")).click();
driver.findElement(By.id(":3")).click();
driver.findElement(By.xpath(".//*[@id='BirthDay']")).sendKeys("14");
driver.findElement(By.xpath(".//*[@id='BirthYear']")).sendKeys("1988");

driver.findElement(By.id("Gender")).click();
driver.findElement(By.id(":e")).click();

driver.findElement(By.xpath(".//*[@id='RecoveryPhoneNumber']")).sendKeys("xxxxxxx");
driver.findElement(By.xpath(".//*[@id='RecoveryEmailAddress']")).sendKeys("xxxxx@gmail.com");
driver.findElement(By.id("submitbutton")).click();

String myWindowHandle = driver.getWindowHandle();
driver.switchTo().window(myWindowHandle);

driver.findElement(By.id("tos-scroll")).click();

//Initialize Javascript executor
JavascriptExecutor js = (JavascriptExecutor) driver;

//Scroll inside web element vertically (e.g. 100 pixel)
js.executeScript("arguments[0].scrollTop = arguments[1];",driver.findElement(By.id("iagreebutton")), 300);



    } }
}

enter image description here

2 个答案:

答案 0 :(得分:0)

本作品:

driver.manage().window().maximize();
    driver.get("https://accounts.google.com/SignUp?service=mail&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&ltmpl=default");
    driver.findElement(By.xpath(".//*[@id='FirstName']")).sendKeys("Krishna");
    driver.findElement(By.xpath(".//*[@id='LastName']")).sendKeys("Krishna");
    driver.findElement(By.xpath(".//*[@id='GmailAddress']")).sendKeys("Krishna.Krishna1154");
    driver.findElement(By.xpath(".//*[@id='Passwd']")).sendKeys("04Mar1989@");
    driver.findElement(By.xpath(".//*[@id='PasswdAgain']")).sendKeys("04Mar1989@");
    //Input the month
    List<WebElement> month_dropdown = driver.findElements(By.xpath(".//*[@id='BirthMonth']/div"));
    //iterate the list and get the expected month
    Thread.sleep(3000);
    for (WebElement month_ele:month_dropdown){
    String expected_month = month_ele.getAttribute("innerHTML");
    // Break the loop if match found

    Thread.sleep(3000);
    if(expected_month.equalsIgnoreCase("August")){
        month_ele.click();
        break;
    }

    driver.findElement(By.id("BirthMonth")).click();
    driver.findElement(By.id(":3")).click();
    driver.findElement(By.xpath(".//*[@id='BirthDay']")).sendKeys("14");
    driver.findElement(By.xpath(".//*[@id='BirthYear']")).sendKeys("1988");

    driver.findElement(By.id("Gender")).click();
    driver.findElement(By.id(":e")).click();

    driver.findElement(By.xpath(".//*[@id='RecoveryPhoneNumber']")).sendKeys("4694222863");
    driver.findElement(By.xpath(".//*[@id='RecoveryEmailAddress']")).sendKeys("sonal.kumar123@gmail.com");
    driver.findElement(By.id("submitbutton")).click();


    Thread.sleep(3000L);
    driver.findElement(By.xpath("//*[@id='tos-scroll-button']/div/img")).click();

答案 1 :(得分:0)

您只需要使用driver.findElement(By.id("tos-scroll")).click();替换代码中的driver.findElement(By.id("tos-scroll-button")).click();,因为所需控件的 id 属性的正确值为'tos-scroll-button'。此外,您可以从代码中删除以下行,因为它们不是必需的:

//Initialize Javascript executor
JavascriptExecutor js = (JavascriptExecutor) driver;

//Scroll inside web element vertically (e.g. 100 pixel)
js.executeScript("arguments[0].scrollTop = arguments[1];

对于其他方案中的滚动,您可以使用以下代码:

WebDriver driver = new FirefoxDriver();
JavascriptExecutor jse = (JavascriptExecutor)driver;
//For scroll down
jse.executeScript("scroll(0, 250);");
//For scroll up
jse.executeScript("scroll(0, -250);");