C
C
Chvalov2019-03-16 20:27:00
Java
Chvalov, 2019-03-16 20:27:00

How to work with multiple elements in Selenium?

For example, there is a search bar (only available on the main page)
I find the element:

WebElement searchForm = selenium.findElement(By.xpath("//input[@name='searchable']"));
I pass the required request and send:
searchForm.sendKeys("Orico technology");
searchForm.submit();

I want to submit the same form but with a new request:
searchForm.sendKeys("Vasya aloo");
searchForm.submit();
In this case, an Exception will pop up because this element is not on the current page (search is available only from the main one) .
What are the solutions to this problem?
Don't offer Jsoup, etc..., the task is slightly different, but the essence is the same

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan, 2019-03-16
Hasanly @azerphoenix

Little information was provided ... but you can do the following:
use a loop ... For example, take an array String{}or LIst<String>
And then something like this code:

String [] keys = {"key1","key2","key3"};
for (Sting key : keys) {
WebElement searchForm = selenium.findElement(By.xpath("//input[@name='searchable']"));
searchForm.sendKeys(key);
searchForm.submit();
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question