如何通过Selenium-WebDriver正确查找元素以进行输入

时间:2019-05-18 00:02:52

标签: java selenium selenium-webdriver webdriver

我正在尝试创建一个程序来自动执行某些下载,但是,在使用Selenium-WebDriver时,我似乎找不到需要登录的元素。我找到了正确的元素,但是实际上使用了WebDriver#findElement()给我一些问题。

<input id="form-username" class="form-field" form="popup-login" type="text" name="username" value="" tabindex="1" autofocus="">

我一直在尝试使用不同的By方法,尽管它们都无效,但是它们都无法与ID一起使用。

我检查了其他帖子,但是似乎都不适合,因为它们只是从HTML的特定点(如字符串)中检索信息,我想在其中输入信息。

public void start(String usernameInfo, String passwordInfo) {
    driver = new HtmlUnitDriver();
    driver.get("https://www.nexusmods.com");

    WebElement username = driver.findElement(By.id("form-username"));
    username.sendKeys(usernameInfo);
    username.submit();

    WebElement password = driver.findElement(By.id("form-password"));
    password.sendKeys(passwordInfo);
    password.submit();

    System.out.println(driver.getTitle());
    driver.quit();
}

可以在这里查看输出日志:https://hastebin.com/zuvebosaha.nginx

更新: 尝试使用ChromeDriver,并找到了以下代码(供我使用)

public void start(String usernameInfo, String passwordInfo) {
    System.setProperty("webdriver.chrome.driver","C:\\Users\\veeay\\Documents\\chromedriver.exe"); //add chrome driver path (System.setProperty("webdriver.chrome.drive",chrome driver path which you downloaded)

    WebDriver driver = new ChromeDriver(); // create object of ChromeDriver

    driver.manage().window().maximize(); // maximize the browser window

    driver.get("https://www.nexusmods.com/"); //enter url

    driver.findElement(By.id("form-username")).sendKeys(usernameInfo); //type textbox's id or name or any locater along with data in sendkeys

    driver.findElement(By.id("form-password")).sendKeys(passwordInfo);

    driver.findElement(By.id("btnLogin")).click();

    try {
        Thread.sleep(2000); //used thread for hold process
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    driver.quit(); //for close browser
}

产生以下结果:https://hastebin.com/iliyuvucok.cs

更新2:奇怪的是,既然我实际发布了问题,我就很好。现在,除了选择登录按钮之外,我可以做所有事情。

public void start(String usernameInfo, String passwordInfo) {
    System.setProperty("webdriver.chrome.driver", "C:\\Users\\veeay\\Documents\\chromedriver.exe"); //add chrome driver path (System.setProperty("webdriver.chrome.drive",chrome driver path which you downloaded)

    WebDriver driver = new ChromeDriver(); // create object of ChromeDriver

    driver.manage().window().maximize(); // maximize the browser window

    driver.get("https://www.nexusmods.com/Core/Libs/Common/Widgets/LoginPopUp?url=%2F%2Fwww.nexusmods.com%2F"); //enter url

    driver.findElement(By.id("form-username")).sendKeys(usernameInfo); //type textbox's id or name or any locater along with data in sendkeys

    driver.findElement(By.id("form-password")).sendKeys(passwordInfo);

    driver.findElement(By.id("sign-in-button")).click();

    try {
        Thread.sleep(2000); //used thread for hold process
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    driver.quit(); //for close browser
}

显然登录按钮不可交互https://hastebin.com/ahuvezoxat.cs

1 个答案:

答案 0 :(得分:0)

添加了显式等待,并且可以正常工作:

package vee;

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

    public class Vee {

        @Test
        public void start() {
            System.setProperty("webdriver.chrome.driver", "C:\\Users\\pburgr\\Desktop\\selenium-tests\\GCH_driver\\chromedriver.exe");

            WebDriver driver = new ChromeDriver();
            driver.manage().window().maximize();

            // new explicit wait
            WebDriverWait webDriverWait = new WebDriverWait(driver, 5);

            driver.get("https://www.nexusmods.com/Core/Libs/Common/Widgets/LoginPopUp?url=%2F%2Fwww.nexusmods.com%2F");

            // using explicit wait
            webDriverWait.until(ExpectedConditions.elementToBeClickable(By.id("sign-in-button")));

            driver.findElement(By.id("form-username")).sendKeys("some name");
            driver.findElement(By.id("form-password")).sendKeys("some password");

            // print true or false by the button state
            System.out.println(driver.findElement(By.id("sign-in-button")).isEnabled());

            driver.findElement(By.id("sign-in-button")).click();

            driver.quit();
        }
    }

输出:

Starting ChromeDriver 74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}) on port 4301
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
[1560240089.419][WARNING]: This version of ChromeDriver has not been tested with Chrome version 75.
Čer 11, 2019 10:01:31 DOP. org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
true

也许在重复测试时,会弹出recaptcha并禁用按钮。