问题
I have little problem with my automated test. When my test make error:
Expected condition failed: waiting for visibility of element located by(...)
and I don't know what is problem.
@BeforeMethod
public void BeforeTest(){
System.setProperty("webdriver.chrome.driver", "C:/drivers/chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.navigate().to("https://poczta.o2.pl/rejestracja/");
}
@Test
public void Test(){
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement Category_Body = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("[name='9cb78dee-04b3-01d5-524b-9159a1646cd3']")));
WebElement user = driver.findElement(By.cssSelector("[name='9cb78dee-04b3-01d5-524b-9159a1646cd3']"));
user.sendKeys("Cezary");
WebElement user2 = driver.findElement(By.cssSelector("input[name*='42aced']"));
user2.sendKeys("Znojek");
WebElement male = driver.findElement(By.id("male"));
((WebElement) male).click();
}
What is my problem?
回答1:
It appears that name 9cb78dee-04b3-01d5-524b-9159a1646cd3
is dynamic. For example when I loaded page now the "first name" element looked like this:
<input type="text" name="2a810eef-304a-e7bb-d148-f9655e639f02" aria-labelledby="0bd5980e-a03a-4dd9-1302-105b999c3afc" class="invalid sc-bZQynM bZqAGh" autocomplete="off" value="">
So you cannot use it to identify the object. In case of that particular form, I would recommend looking for label and then take input field associated with it. They are both included in the same div, so quite distinguishable:
<div class="">
<label id="790a1161-7317-afc5-7563-1973254ccc96" class="sc-dnqmqq dsXufX">Imię</label>
<input type="text" name="885f994f-f81e-7142-c3f0-61420449b0b7" aria-labelledby="790a1161-7317-afc5-7563-1973254ccc96" class="sc-bZQynM bZqAGh" autocomplete="off" value="">
</div>
In XPath it could be expressed like this:
//label[text()='Imię']/../input
(select label, return to parent and then select input field under the same parent)
Such selection is also much better semantically, because it's very clear what you are selecting (name field), as opposed to selection by GUID, which doesn't tell you what it is you are selecting.
In CSS selector I'm afraid this is not possible since it doesn't support selection by text (I might be wrong, since CSS selectors are not my forte).
来源:https://stackoverflow.com/questions/53353370/selenium-webdriver-problem-with-expected-condition-failed-waiting-for-visibili