简体   繁体   English

使用带有Java和Chrome的WebDriver Selenium 3,无法将sendKeys放入ebay用户名字段

[英]Cant sendKeys into ebay username field using WebDriver Selenium 3 with Java and chrome

I have been unable to sendKeys text into the username and password field on ebay. 我一直无法将eKeys文本发送到ebay上的用户名和密码字段中。

Here is the code: 这是代码:

    WebDriver driver = null;
    System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver");
    driver = new ChromeDriver();
    driver.get("http://www.ebay.co.uk");

    WebElement myEbay = driver.findElement(By.linkText("My eBay"));
    myEbay.click();

    WebElement signInForm = driver.findElement(By.id("SignInForm"));

    if (signInForm.isDisplayed())
        System.out.println("Sign in form is displayed");

    WebElement username;

    username = driver.findElement(By.cssSelector("input[placeholder=\"Email or username\"]"));

It manages to find the My Ebay link, and verifies that the sign in form exists but the the username and password fields id's change after every refresh of the page. 它设法找到“我的Ebay”链接,并验证登录表单是否存在,但每次刷新页面后用户名和密码字段ID均已更改。

The username cssSelector seems to be the problem?? 用户名cssSelector似乎是问题所在?

EDIT: I have been successful using XPath but this excercise was to make the cssSelector work as there is no reason in theory why it shouldn't! 编辑:我已经成功使用XPath,但是这个练习是使cssSelector工作,因为从理论上讲没有理由不这样做!

Select all inputs as a list and search for the right one: 选择所有输入作为列表,然后搜索正确的输入:

// fld seems to be the class of the input field but looking at all input elements should work too
List<WebElement> inputs = driver.findElements(By.className("fld")); 
for (WebElement input: inputs) { 
    if (inputs.getAttribute("placeholder") == "Email or username") {
        // ...
    }
}

if the username and password webelements changes frequently find div section in the page using xpath and get the exact xpath of username weblement. 如果用户名和密码Webelements经常更改,请使用xpath在页面中查找div部分,并获取用户名weblement的确切xpath。

Hope this help.. 希望有帮助。

I have replaced the cssSelector locator with xpath and it is working fine with absolute xpath. 我已经用xpath替换了cssSelector定位器,并且在绝对xpath下可以正常工作。 Here is the modified code: 这是修改后的代码:

 WebDriver driver = null;
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\vikas\\workspaceNeon\\Eclipse Soft\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.get("http://www.ebay.co.uk");

        WebElement myEbay = driver.findElement(By.linkText("My eBay"));
        myEbay.click();

        WebElement signInForm = driver.findElement(By.id("SignInForm"));

        if (signInForm.isDisplayed())
            System.out.println("Sign in form is displayed");

       driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

       WebElement username = driver.findElement(By.xpath("html/body/div[4]/div/div/div/div[5]/div/div[1]/div/div[1]/div[1]/div[2]/div/span/form/div[1]/div[5]/div/div[4]/span[2]/input"));

       username.sendKeys("Vikas");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM