Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Selenium 无法向下滚动弹出消息-Gmail登录_Selenium_Selenium Webdriver - Fatal编程技术网

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

Selenium 无法向下滚动弹出消息-Gmail登录,selenium,selenium-webdriver,Selenium,Selenium Webdriver,我正在尝试自动登录gmail页面。一旦我输入所有必需的详细信息,如用户名、通行证、出生日期、电子邮件等,然后单击“下一步”按钮,我会收到一条弹出消息,需要向下滚动到最后,然后启用“接受”按钮 我无法向下滚动弹出页面。下面是我的代码 //import org.apache.bcel.generic.Select; import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.JavascriptE

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

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

//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);



    } }
}
//导入org.apache.bcel.generic.Select;
导入java.util.List;
导入org.openqa.selenium.By;
导入org.openqa.selenium.JavascriptExecutor;
导入org.openqa.selenium.Keys;
导入org.openqa.selenium.WebDriver;
导入org.openqa.selenium.WebElement;
导入org.openqa.selenium.firefox.FirefoxDriver;
导入org.openqa.selenium.interactions.Actions;
导入org.openqa.selenium.support.ui.ExpectedConditions;
导入org.openqa.selenium.support.ui.Select;
导入org.openqa.selenium.support.ui.WebDriverWait;
导入com.gargoylesoftware.htmlunit.javascript.host.Window;
公共类Gmail登录{
公共静态void main(字符串[]args)引发InterruptedException{
//TODO自动生成的方法存根
WebDriver=newfirefoxdriver();
//最大化浏览器
driver.manage().window().maximize();
驱动程序。获取(“https://accounts.google.com/SignUp?service=mail&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F<mpl=default");
driver.findelelement(By.xpath(“./*[@id='FirstName'])).sendKeys(“Krishna”);
driver.findelelement(By.xpath(“./*[@id='LastName'])).sendKeys(“Krishna”);
driver.findElement(By.xpath(“./*[@id='GmailAddress'])).sendKeys(“Krishna.Krishna1154”);
driver.findelelement(By.xpath(“./*[@id='Passwd']”)。sendKeys(“04Mar1989@”);
driver.findelelement(By.xpath(“./*[@id='passwdreach']”);
//输入月份
List month_dropdown=driver.findElements(By.xpath(“./*[@id='BirthMonth']]/div”);
//迭代列表并获得预期月份
睡眠(3000);
for(WebElement月元素:月下拉列表){
字符串应为\u month=month\u ele.getAttribute(“innerHTML”);
//如果找到匹配项,则中断循环
睡眠(3000);
如果(预计为8月份){
月份元素单击();
打破
}
driver.findElement(By.id(“生日”))。单击();
driver.findElement(By.id(“:3”)。单击();
driver.findelelement(By.xpath(“./*[@id='birth'])).sendKeys(“14”);
driver.findelelement(By.xpath(“./*[@id='BirthYear'])).sendKeys(“1988”);
driver.findElement(By.id(“性别”))。单击();
driver.findElement(By.id(“:e”)。单击();
driver.findelelement(By.xpath(“./*[@id='RecoveryPhoneNumber']”);
driver.findElement(By.xpath(“../*[@id='RecoveryEmailAddress']”)的驱动程序。sendKeys('xxxxx@gmail.com");
driver.findElement(By.id(“submitbutton”))。单击();
字符串myWindowHandle=driver.getWindowHandle();
driver.switchTo().window(myWindowHandle);
driver.findElement(By.id(“tos滚动”))。单击();
//初始化Javascript执行器
JavascriptExecutor js=(JavascriptExecutor)驱动程序;
//垂直滚动web元素内部(例如100像素)
js.executeScript(“arguments[0].scrollTop=arguments[1];”,driver.findElement(By.id(“iagreebutton”)),300);
} }
}

这项工作:

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();
driver.manage().window().maximize();
驱动程序。获取(“https://accounts.google.com/SignUp?service=mail&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F<mpl=default");
driver.findelelement(By.xpath(“./*[@id='FirstName'])).sendKeys(“Krishna”);
driver.findelelement(By.xpath(“./*[@id='LastName'])).sendKeys(“Krishna”);
driver.findElement(By.xpath(“./*[@id='GmailAddress'])).sendKeys(“Krishna.Krishna1154”);
driver.findelelement(By.xpath(“./*[@id='Passwd']”)。sendKeys(“04Mar1989@”);
driver.findelelement(By.xpath(“./*[@id='passwdreach']”);
//输入月份
List month_dropdown=driver.findElements(By.xpath(“./*[@id='BirthMonth']]/div”);
//迭代列表并获得预期月份
睡眠(3000);
for(WebElement月元素:月下拉列表){
字符串应为\u month=month\u ele.getAttribute(“innerHTML”);
//如果找到匹配项,则中断循环
睡眠(3000);
如果(预计为8月份){
月份元素单击();
打破
}
driver.findElement(By.id(“生日”))。单击();
driver.findElement(By.id(“:3”)。单击();
driver.findelelement(By.xpath(“./*[@id='birth'])).sendKeys(“14”);
driver.findelelement(By.xpath(“./*[@id='BirthYear'])).sendKeys(“1988”);
driver.findElement(By.id(“性别”))。单击();
driver.findElement(By.id(“:e”)。单击();
findElement(By.xpath(“./*[@id='RecoveryPhoneNumber']”);
driver.findelelement(By.xpath(“./*[@id='recoveryemaildress'])).sendKeys(“sonal。kumar123@gmail.com");
driver.findElement(By.id(“submitbutton”))。单击();
线程。睡眠(3000L);
findelelement(By.xpath(“//*[@id='tos-scroll-button']/div/img”)。单击();

您只需更换
driver.findElement(By.id(“tos滚动”))。单击();
在代码中使用
driver.findElement(By.id(“tos滚动按钮”)。单击();
,因为所需控件的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);");

您可能想删除您的个人信息嗨..谢谢,或者您的响应和它的向下滚动,但它不会向下滚动到页面的结尾。一旦向下滚动到结尾,那么只有“我接受”按钮将被启用。我尝试了下面的代码双击,但直到endActions action=new Actions(驱动程序)才滚动;action.moveToElement(driver.findElement(By.id(“tos滚动按钮”)))。双击().build().perform();