@Olga_AQA

Как в Selenium Java получить в элементах интрересующие атрибуты (метод findElements()), а потом нажать, в любой элемент, который подходит?

Мне нужно получить атрибуты "class" во всех элементах локатора, т.ё.получить список. Затем этот список отфильтровать, чтобы не было в атрибуте "class" - "form-disabled", а далее кликать в любой элемент, который не содержит "form-disabled".
Как это сделать?
private static List time;
time = driver.findElements(By.xpath(TIME_CHECK)).iterator().next().getAttribute("class");
Получаю ошибку компиляции стринги и List Как правильно получить атрибуты и записать их?
Затем создаю событие, что жду, если атрибуты не содержат "form-disabled"
WebDriverWait wait = new WebDriverWait(driver, timeout);
ExpectedCondition condition =
ExpectedConditions.not(ExpectedConditions.attributeContains(By.xpath(TIME_CHECK), "class", "form-disabled"));
Если дождалась кликаю в любой элемент
if (wait.until(condition)){
time = driver.findElements(By.xpath(TIME_CHECK));
int indexOfRandomElement = new Random().ints(0, time.size() + 1)
.findFirst()
.getAsInt();
time.get(indexOfRandomElement).click();
} else {
driver.findElement(By.xpath(ANOTHER_DAY_BUTTON_XPATH)).click();
Код проверить не могу, т.к. сразу вначале ошибку компиляции получаю.
  • Вопрос задан
  • 120 просмотров
Пригласить эксперта
Ответы на вопрос 1
@Miron11
Пишу sql 20 лет. Срок :)
I think you just dialed a bit too far with
time = driver.findElements(By.xpath(TIME_CHECK)).iterator().next().getAttribute("class");
with . approach.
Usually what we do, if you do want to use this convoluted one - liner ( in c# for example ) is call ( this is a pseudocode )
.iterator().firstOrDefault()
and that way we get the value.
In Java I think there are methods such as tail(x) / head(x) / first(x) invoked on the collection ( here it is probably the return of findElement()) which will return an array from which you can take [0] element ( which may fail to null exception )
The Java pattern is to first check for null and than perform value access.
So, you can see that while one - liner can look nice, it would require a bit higher effort at understanding internals.
Simpler pattern is to scroll using iterator
while(driver.findElements(By.xpath(TIME_CHECK)).iterator().next()) {
x = driver.findElements(By.xpath(TIME_CHECK)).iterator().current() / item()
myValue = x.method / property
break or something else
}
This takes a few more lines, but is easy to follow and debug. Personally, I use both approaches, depending on context, but when in doubt the easier one.
---
I was under impression this is where error occurs ( please correct me if I am wrong ).
If my understanding is correct, the rest looks like somewhat working approach.
Except it's probably an pattern specific to Selenium.
In an application, if I wanted to be able to perform some action on items, found in the cycle of the iterator above, while cycle is still running, I'd use a
yeld
pattern, and organize concurrency by means of function invoked in yeld block with item / value passed as parameter. Give me a moment to find an example.
Darn it, yeld is c# pattern, ported to java only as of release 13.
So, just save the value to list in the cycle and run over it once iterator is done.
It shouldn't take too long.
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы