Selenium HTMLUnitDriver无法登录

Selenium HTMLUnitDriver无法登录,selenium,htmlunit,Selenium,Htmlunit,更新 我正在尝试登录此网站: 不幸的是,我没有成功。这是我的密码: WebDriver driver = new HtmlUnitDriver(); driver.get("https://trailhead.salesforce.com/en/home"); System.out.println(driver.getTitle()); WebElement btnLogin = driver.findElement(By.xpath("//*[@id=\"main-wrapper\"]/

更新

我正在尝试登录此网站:

不幸的是,我没有成功。这是我的密码:

WebDriver driver = new HtmlUnitDriver();

driver.get("https://trailhead.salesforce.com/en/home");
System.out.println(driver.getTitle());

WebElement btnLogin = driver.findElement(By.xpath("//*[@id=\"main-wrapper\"]/header/div[1]/div[2]/span[2]/div/span/button"));
System.out.println(btnLogin);

driver.quit();
我试图从标题中获取登录按钮,但找不到元素这是我获取的异常消息:

线程“main”org.openqa.selenium.NoSuchElementException中的异常: 无法使用找到节点 //*[@id=“main wrapper”]/header/div/div[2]/span[2]/div/span/button

我试着从下面测试答案,但是,我没有成功。我想更好地了解WebDriverWait的工作原理和until()方法


谢谢。

问题是有多个登录按钮。您的代码等待第一个按钮可见。不幸的是,它不会。所以Selenium一直等到超时
我建议创建一个方法,通过遍历所有saleforce登录按钮并检查属性
isDisplayed()
来找到正确的按钮。如果是,请使用按钮。请参见下面的示例

private WebElement findLoginButton(WebDriver d, By by) {
    List<WebElement> elements = d.findElements(by);
    for (Iterator<WebElement> e = elements.iterator(); e.hasNext();) {
        WebElement item = e.next();
        if(item.isDisplayed())
                return item;
    }
    return null;
}

@Test
public void testSaleforce() {
    WebDriver driver = new HtmlUnitDriver();
    driver.get("https://trailhead.salesforce.com/en/home");
    System.out.println(driver.getTitle());
    WebElement btnLogin = driver.findElement(By.xpath("//*[@data-react-class='auth/LoginModalBtn']"));
    System.out.println(btnLogin);
    btnLogin.click();
    WebDriverWait wait = new WebDriverWait(driver,10);
    WebElement btnLoginSalesforce = wait.until((WebDriver d) -> findLoginButton(d,By.cssSelector(".th-modal-btn__salesforce")));
    btnLoginSalesforce.click();
    ...
}
私有WebElement findLoginButton(WebDriver d,By){
列表元素=d.findElements(按);
for(迭代器e=elements.Iterator();e.hasNext();){
WebElement item=e.next();
if(item.isDisplayed())
退货项目;
}
返回null;
}
@试验
公共无效testSaleforce(){
WebDriver driver=新的HtmlUnitDriver();
驱动程序。获取(“https://trailhead.salesforce.com/en/home");
System.out.println(driver.getTitle());
WebElement btnLogin=driver.findElement(By.xpath(“/*[@data react class='auth/LoginModalBtn']);
System.out.println(btnLogin);
btnLogin.click();
WebDriverWait wait=新的WebDriverWait(驱动程序,10);
WebElement btnLoginSalesforce=wait.until((WebDriver d)->findLoginButton(d,By.cssSelector(.th-modal-btn_usalesforce));
btnLoginSalesforce.click();
...
}

在本网页中,开发人员非常友好地为HTML添加了特殊(HTML5)属性,用于测试目的,称为“数据测试”。借助这些属性,您可以使用CSS选择器在Selenium代码中定位Webelements。因此不需要复杂的Xpath选择器,也不需要像@Buaban建议的那样遍历按钮

我测试了下面的C#代码,并点击了登录按钮

 IWebElement btnLogin = Driver.FindElement(By.CssSelector("button[data-test=header-login]"));

 btnLogin.Click();
我认为您正在使用Java,那么它将是:

 WebElement btnLogin = driver.findElement(By.cssSelector("button[data-test=header-login]"));

 btnLogin.click();
要获得更稳定的解决方案,必须使用显式等待:

WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.css(""button[data-test=header-login]"")));

Selenium将尝试每500毫秒查找一次按钮,最多20秒。在找到按钮或经过20秒之前,将忽略NoTouchElementException。如果在20秒后未找到按钮,将引发超时异常。

您试图用
(By.xpath(“/*[@data react class='auth/LoginModalBtn']”)做什么。
您说您没有成功,到底出了什么问题?是否在哪一行引发异常?我们需要更多的信息来帮助您。@DebanjanB我用它来找到主页上的登录按钮,以打开登录选项。这部分工作正常。@Frank我的代码的问题是,我想登录到Trailhead帐户,但在插入电子邮件和密码并提交表单后,我会在登录用户的主页上,但是,没有。@lbpeppers那么你到底被困在哪里?还是你的问题已经解决了?谢谢你的回答,我正在测试你的答案。谢谢你的回答,弗兰克,我会测试这个答案并尽快回复。我现在正在做一个不同的项目。谢谢你,已经测试过了,工作得很好。弗兰克,你知道为什么这段代码可以和Chrome驱动程序一起工作,而不能和HTML单元驱动程序一起工作吗?