【发布时间】:2016-08-08 18:52:46
【问题描述】:
使用 xpath 定位
但在 EclipseIDE 中显示异常
WebElement sett = dr.findElement(By.xpath("//div[@data-tooltip='Settings']"));
sett.click();
but showing exception in EclipseIDE
.
【问题讨论】:
标签: firefox xpath gmail firebug
使用 xpath 定位
但在 EclipseIDE 中显示异常
WebElement sett = dr.findElement(By.xpath("//div[@data-tooltip='Settings']"));
sett.click();
but showing exception in EclipseIDE
.
【问题讨论】:
标签: firefox xpath gmail firebug
NoSuchElementException 发生在您正在查找的元素在解析请求时不存在于 DOM 中时。通常有两个原因:
第二个很简单。验证您的 XPath(使用 FirePath 等工具)。
如果您的 XPath 是正确的,那么它很可能是第一个。在这种情况下 WebDriverWait 或 PageFactory 会有所帮助。
WebDriverWait 是您的测试的内联解决方案,它允许查找在失败之前重试特定时间范围。
WebDriverWait wait = new WebDriverWait(dr, 10); //10 second wait
WebElement sett = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@data-tooltip='Settings']"));
sett.click();
这应该考虑到页面加载时间。
请注意,WebDriverWait 将保持配置的最大值 时间,但如果满足条件可以提前释放! 始终使用 等待 Thread.sleep
第二个选项是 PageFactory,它利用 Selenium API 的附加功能和定义明确的 API 来支持具有通用实现的多个测试。这是一个非常灵活的解决方案,可以支持需要执行常见交互的一组测试!
public class PageWithSettings {
@FindBy(xpath="//div[@data-tooltip='Settings']")
WebElement settings;
public void openSettings() {
//Settings will resolve on-demand through the Selenium framework
settings.click();
}
}
然后在您的测试中使用它来实现共享的通用实现。
public class MySeleniumTest {
@Test
public void openSettings() {
WebDriver driver = new FirefoxDriver();
driver.get("myPageWithSettings");
// Use the PageFactory to stand up the reference.
PageWithSettings pageSettings = PageFactory.initElements(driver, PageWithSettings.class);
//Call the API method to open the settings page.
pageSettings.openSettings();
}
}
首先,我设置了三个页面工厂。
电子邮件地址条目
package gmail;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
public class EmailEntryPage {
@FindBy(id = "Email")
private WebElement emailAddress;
@FindBy(id = "next")
private WebElement next;
public void enterEmailAddress(String gmailAccount) {
emailAddress.sendKeys(gmailAccount);
next.click();
}
}
密码输入
package gmail;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
public class AccountPasswordPage {
@FindBy(id = "Passwd")
private WebElement accountPass;
@FindBy(id = "signIn")
private WebElement signIn;
public void enterPassword(String passWd) {
accountPass.sendKeys(passWd);
signIn.click();
}
}
具有设置按钮的页面
package gmail;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
public class PageWithSettings {
@FindBy(id=":2m")
private WebElement settingsDropDown;
@FindBy(id=":2o")
private WebElement densityComfortable;
@FindBy(id=":2p")
private WebElement densityCozy;
@FindBy(id=":2q")
private WebElement densityCompact;
@FindBy(id="101:settings")
private WebElement configureInbox;
@FindBy(id="ms")
private WebElement settings;
@FindBy(id="pbwc")
private WebElement themes;
@FindBy(id=":2r")
private WebElement help;
public void setDensityComfortable() {
settingsDropDown.click();
densityComfortable.click();
}
public void setDensityCozy() {
settingsDropDown.click();
densityCozy.click();
}
public void setDensityCompact() {
settingsDropDown.click();
densityCompact.click();
}
public void configureInbox() {
settingsDropDown.click();
configureInbox.click();
}
public void openSettings() {
settingsDropDown.click();
settings.click();
}
public void openThemes() {
settingsDropDown.click();
themes.click();
}
public void openHelp() {
settingsDropDown.click();
help.click();
}
}
然后我做了一个测试来验证交互:
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.PageFactory;
import gmail.AccountPasswordPage;
import gmail.EmailEntryPage;
import gmail.PageWithSettings;
public class GmailSignInWithSettings {
private final String MY_EMAIL = "";
private final String MY_ACCOUNT_PASS= "";
private WebDriver driver;
@Before
public void constructDriver() {
driver = new FirefoxDriver();
}
@After
public void destroyDriver() throws Exception {
//Sleep call here for demo purposes only!
Thread.sleep(5000);
driver.close();
}
@Test
public void testSignIn() {
driver.get("gmail.com");
EmailEntryPage emailPage = PageFactory.initElements(driver, EmailEntryPage.class);
emailPage.enterEmailAddress(MY_EMAIL);
AccountPasswordPage passPage = PageFactory.initElements(driver, AccountPasswordPage.class);
passPage.enterPassword(MY_ACCOUNT_PASS);
PageWithSettings settingsPage = PageFactory.initElements(driver, PageWithSettings.class);
/*
* Now Do something with the drop-down
*
settingsPage.setDensityComfortable();
settingsPage.setDensityCompact();
settingsPage.setDensityCozy();
settingsPage.configureInbox();
settingsPage.openSettings();
settingsPage.openThemes();
settingsPage.openHelp();
*/
}
}
这似乎对我有用,但我没有彻底测试代码。我可能忽略了一些事情。 Firefox 45.0.1 版
希望对您有所帮助。
【讨论】: