使用xpath定位
但在EclipseIDE中显示异常
WebElement sett = dr.findElement(By.xpath(“// div [@ data-tooltip ='Settings']”));
sett.click();
but showing exception in EclipseIDE
答案 0 :(得分:0)
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
希望有所帮助。