出现此错误:
org.openqa.selenium.TimeoutException:预期条件失败: 等待By.xpath定位的元素的可见性: // select [@ id ='register-dob-month'] / option [text()='5'](尝试5 秒(以500毫秒为间隔)
SignUpPage:
public class SignUpPage {
private WebDriver driver;
public SignUpPage(WebDriver driver) {
this.driver = driver;
}
private By monthDropDown = By.cssSelector("select#register-dob-month");
private String monthDropDownOption = "//select[@id='register-dob-month']/option[text()='%s']";
private By dayField = By.cssSelector("input#register-dob-day");
private By yearField = By.cssSelector("input#register-dob-year");
private String genderRadioButton = "//li[@id='li-gender']/label[normalize-space()='Male']/input";
private By shareCheckbox = By.cssSelector("input#register-thirdparty");
public SignUpPage setMonth(String month) {
driver.findElement(monthDropDown).click();
new WebDriverWait(driver, 5).until(visibilityOfElementLocated(xpath(format(monthDropDownOption, month)))).click();
return this;
}
public SignUpPage typeDay(String day) {
driver.findElement(dayField).sendKeys(day);
return this;
}
public SignUpPage typeYear(String year) {
driver.findElement(yearField).sendKeys(year);
return this;
}
SignUpTest:
public class SignUpTest {
WebDriver driver;
SignUpPage page;
@Before
public void setUp(){
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
driver.get("https://www.spotify.com/int/signup");
}
@Test
public void typeInvalidYear(){
page = new SignUpPage(driver);
page.setMonth("5")
.typeDay("20")
.typeYear("88");
Assert.assertTrue(page.isErrorVisible("Please enter a valid year."));
Assert.assertFalse(page.isErrorVisible("When were you born?"));
}
@After
public void tearDown() {
driver.quit();
}
答案 0 :(得分:0)
发生此错误是因为您在xpath中使用了“ text()”,但应使用“ //选择[@ id ='register-dob-month'] / option [@ value = '05']“ 查看您的HTML(屏幕截图-http://prntscr.com/k539il) 要么 使用“ 五月”,“ 四月”代替月份数。 此外,您可以尝试使用Selenium Select类在此下拉菜单中进行操作:
WebElement Dropdown = driver.findElement(By.cssSelector("select#register-dob-month"));
Select select = new Select(Dropdown);
select.selectByIndex(4);
//or
select.deselectByVisibleText("May");