Java 如何在登录yahoo mail时单击“下一步”按钮?

Java 如何在登录yahoo mail时单击“下一步”按钮?,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,我正在尝试登录雅虎邮件。无法继续单击“下一步”按钮。我无法找到我失踪的地方。请引导我。多谢各位 import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public c

我正在尝试登录雅虎邮件。无法继续单击“下一步”按钮。我无法找到我失踪的地方。请引导我。多谢各位

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class FirstScript {
    public static void main (String[] args) {
        System.setProperty("webdriver.chrome.driver","C:\\Selenium\\selenium-java-            3.141.59\\chromedriver_win32\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.manage().deleteAllCookies();
        driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.get("https://login.yahoo.com/");                                                                             driver.findElement(By.name("username")).sendKeys("test@yahoo.com"); //name locator for text box
        WebElement searchIcon = driver.findElement(By.name("signin"));//name locator for next button
        searchIcon.click();
        driver.findElement(By.name("password")).sendKeys("testing"); //name locator for text box
        WebElement searchIcon2 =     driver.findElement(By.name("verifyPassword"));//name locator for next button
        searchIcon2.click();
    }
}

你的脚本是正确的。您只需等待屏幕重定向到下一页即可。修改后的脚本(包括显式等待)如下所示

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class sample{
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver","<driverlocation>");
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        WebDriverWait wait = new WebDriverWait(driver,30);
        driver.manage().deleteAllCookies();
        driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.get("https://login.yahoo.com/");
        driver.findElement(By.name("username")).sendKeys("test@yahoo.com"); // name locator for text box
        WebElement searchIcon = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"login-signin\"]")));// name locator for next button
        searchIcon.click();
        WebElement password = wait.until(ExpectedConditions.presenceOfElementLocated(By.name("password")));
        password.sendKeys("testing"); // name locator for text box
        WebElement searchIcon2 = driver.findElement(By.name("verifyPassword"));// name locator for next button
        searchIcon2.click();
    }
}`
import java.util.concurrent.TimeUnit;
导入org.openqa.selenium.By;
导入org.openqa.selenium.Keys;
导入org.openqa.selenium.WebDriver;
导入org.openqa.selenium.WebElement;
导入org.openqa.selenium.chrome.ChromeDriver;
导入org.openqa.selenium.support.ui.ExpectedConditions;
导入org.openqa.selenium.support.ui.WebDriverWait;
公共类样本{
公共静态void main(字符串[]args){
System.setProperty(“webdriver.chrome.driver”和“”);
WebDriver驱动程序=新的ChromeDriver();
driver.manage().window().maximize();
WebDriverWait wait=新的WebDriverWait(驱动程序,30);
driver.manage().deleteAllCookies();
driver.manage().timeout().pageLoadTimeout(40,TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
驱动程序。获取(“https://login.yahoo.com/");
driver.findElement(按.name(“用户名”)).sendKeys(“test@yahoo.com“”;//文本框的名称定位器
WebElement searchIcon=wait.until(ExpectedConditions.elementToBeClickable(By.xpath(“//*[@id=\”登录登录\“]”));//下一步按钮的名称定位器
搜索图标。单击();
WebElement password=wait.until(ExpectedConditions.presenceOfElementLocated(By.name(“密码”));
password.sendKeys(“testing”);//文本框的名称定位器
WebElement searchIcon2=driver.findElement(By.name(“verifyPassword”);//下一步按钮的名称定位器
searchIcon2.单击();
}
}`

要首先向电子邮件地址字段发送字符序列,调用按钮上的
单击()
,文本为下一步,向密码字段发送字符序列,然后再次调用按钮上的
单击()
,文本为下一步,您需要为
元素tobelickable()
引入WebDriverWait,并且可以使用以下定位器策略:

  • 代码块:

    import org.openqa.selenium.By;
    import org.openqa.selenium.JavascriptExecutor;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    public class A_ChromeTest 
    {
        public static void main(String[] args) 
        {
            System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
            ChromeOptions options = new ChromeOptions();
            options.addArguments("start-maximized");
            options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
            options.setExperimentalOption("useAutomationExtension", false);
            WebDriver driver =  new ChromeDriver(options);
            driver.navigate().to("https://login.yahoo.com/");
            new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.phone-no#login-username"))).sendKeys("dev_pspl");
            driver.findElement(By.cssSelector("input#login-signin")).submit();
            new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div#password-container>input"))).sendKeys("test123");
            ((JavascriptExecutor)driver).executeScript("arguments[0].click();", driver.findElement(By.xpath("//button[@id='login-signin']")));
        }
    }
    
  • 浏览器快照:


我尝试了下面的代码,所有这些代码都非常有效,值得一试

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("https://login.yahoo.com/");
driver.findElement(By.cssSelector("#login-username")).sendKeys("Test123456789");    
driver.findElement(By.cssSelector("#login-signin")).click();
driver.findElement(By.cssSelector("#login-passwd")).sendKeys("123456");
driver.findElement(By.cssSelector("#login-signin")).click();

在输入用户名后仍无法继续。它在用户名处卡住了。你能提供当它卡住时引发的异常吗。线程“main”org.openqa.selenium.TimeoutException中的异常:预期条件失败:等待位于org.openqa.selenium.support.ui.WebDriverWait.TimeoutException(WebDriverWait.java:95)处的:by.name:password(尝试30秒,间隔500毫秒)的元素出现在org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:272)的edureka.FirstScript.main(FirstScript.java:28)上,由以下原因引起:org.openqa.selenium.NoSuchElementException:无此类元素:无法找到元素:{“方法”:“css选择器”,“选择器”:“*[name='password']”获取错误-线程“main”中出现异常错误:未解决的编译问题:无法将JavascriptExecutor解析为类型JavascriptExecutor无法解析为type@Ravi签出更新的答案并让我知道线程“main”org.openqa.selenium.NoSuchElementException中的status.Exception:没有这样的元素:无法找到元素:{“method”:“css选择器”,“选择器”:“#登录密码”}(会话信息:chrome=80.0.3987.122)有关此错误的文档,请访问:Build info:version:'3.141.59',revision:'e82be7d358',time:'2018-11-14T08:25:48',了解有关等待的更多信息。如果完全相同的脚本对我有效,但对你无效,这意味着当selenium单击
#登录密码
时,它不在那里。因此,你可能需要尝试arti中解释的显式等待克莱