I have a method in which I am trying to add 12 web elements:
private List<WebElement> iphoneSnippetList = new ArrayList<>();
#Test
public void test(){
chromeDriver.get("https://market.yandex.ru/catalog--smartfony/54726/list?hid=91491&glfilter=7893318%3A153043&onstock=1&local-offers-first=0");
new WebDriverWait(chromeDriver, 15).until(ExpectedConditions.elementToBeClickable(By.xpath("//article[#data-autotest-id='product-snippet'][1]")));
for (int i = 0; i <= 12; i++) {
iphoneSnippetList.add((WebElement) By.xpath("//article[#data-autotest-id='product-snippet'][" + i + "]"));
}
System.out.println(iphoneSnippetList);
}
Simplified DOM elements in which I only need to get the text :
<article class="_2vCnw cia-vs cia-cs" data-autotest-id="product-snippet"</article>
<article class="_2vCnw cia-vs cia-cs" data-autotest-id="product-snippet"</article>
<article class="_2vCnw cia-vs cia-cs" data-autotest-id="product-snippet"</article>
I need to add all 12 web elements to my array and then make sure that the received elements contain the name "Iphone", but when adding elements, there is exception:
java.lang.ClassCastException: class org.openqa.selenium.By$ByXPath cannot be cast to class org.openqa.selenium.WebElement (org.openqa.selenium.By$ByXPath and org.openqa.selenium.WebElement are in unnamed module of loader 'app')
iphoneSnippetList is a List of WebElement in Java-Selenium bindings.
I am not sure why you want to add 12 web elements using the loop, instead a findElements with right xpath would have been a good choice. Anyway, there's a problem with you code related to casting.
See below, driver.findElement will return the web element, and we are storing that into a variable called Webelement e, and adding the same into iphoneSnippetList
for (int i = 0; i <= 12; i++) {
WebElement e = driver.findElement(By.xpath("//article[#data-autotest-id='product-snippet'][" + i + "]"));
iphoneSnippetList.add(e);
}
System.out.println(iphoneSnippetList);
Also, this loop will run for 13 times not 12 times. In case you want this to run for 12 times, initialize i = 1 not i = 0
I think you will have issue with xpath also, cause you are not using xpath indexing correctly.
try this instead :
for (int i = 1; i <= 12; i++) {
WebElement e = driver.findElement(By.xpath("(//article[#data-autotest-id='product-snippet'])['" +i+ "']"));
iphoneSnippetList.add(e);
}
System.out.println(iphoneSnippetList);
Related
Select se= new select(locator of drop-down);
List<webelemts> list= se.getoptions();
For(int l: list){}
Not sure as I want to select all countries starts ends with land and count the no. Of them and print.
You can simply create a int counter and initial value will be set to 0.
also using the below for each loop, you can iterate all the options and using .getText() method, you can use .startsWith or .endsWith like below.
int counter = 0;
Select se = new Select(driver.findElement(By.xpath("select web element xpath here")));
List<WebElement> list = se.getOptions();
for (WebElement ele : list) {
if(ele.getText().startsWith("land") || ele.getText().endsWith("land")) {
counter++;
}
}
and then finally print the counter,
System.out.println(counter);
Hi I just want to get the index number of TH column using unique Xpath. Like A loop will run and check on which TH column Xpath match and return the index number. Is there any way i can do this in selenium ? Till now im able to get the tag index and run the the loop but now I have check the on each TH with xpath rather its matched or not and give me the index number. Please let me know is there anything with Im able to achieve this with this logic or any other any Xpath techniques.
Table = driver.findElement(By.xpath("//table/thead[#class='ui-datatable-thead']"))
List<WebElement> rows_head = Table.findElements(By.tagName('th'))
int head_size= rows_head.size()
System.out.println(head_size);
for (int c = 1; c <= head_size; c++) {
driver.findElement(By.xpath(
"//th[4][#class='ui-state-default ui-unselectable-text ui-sortable-column']")
)
// Here is something I want loop will check the each TH with above given
// Xapth and return the TH index in the table on match TH xpath index.
}
Selenium doesn't supply an API to return the xpath of found element. So you can't archive your goal by comparing xpath.
But Selenium supply API to get the attribute value of found element, you can compare with it to see it's your desired element or not.
WebElement thead = driver.findElement(By.xpath("//table/thead[#class='ui-datatable-thead']"));
List<WebElement> heads = thead.findElements(By.tagName('th'));
int head_size= heads.size()
System.out.println(head_size);
String expectedClass = "ui-state-default ui-unselectable-text ui-sortable-column"
int i = 1;
for (; i <= head_size; i++) {
if(heads[i].getAttribute("class").equal(expectedClass))
// check attribute class value is as expect
// you can change to other attribute or check more than one attribute
break;
)
}
System.out.println("Matched th index: " + i);
#AndroidFindBy (uiAutomator = "new UiSelector().className(\"android.support.v7.widget.RecyclerView\").childSelector(new UiSelector().className(\"android.widget.RelativeLayout\"))")
public List<MobileElement> listOfElements;
System.out.print(listOfElements.size());
This returns 1.
So there is an element "android.support.v7.widget.RecyclerView" which contains 9 elements "android.widget.RelativeLayout". Those I want to get a list of, but I get only 1 element with the aforementioned locator. What am I doing wrong here?
If I add .index() at the end of locator, then it will give me an element according to specified index number, but I need a list of all child elements.
And does appium support all UiSelector commands? Because some of them do not seem to be working (like classNameMatches where you can type a regex, or fromParent, scrollable, ...)
you can use parent , child class strategy as below,
WebElement l1 = driver.findElementByClassName("android.support.v7.widget.RecyclerView\");
//All elements in big container L1
List<WebElement> l2 = l1.findElements(By.className("android.widget.RelativeLayout\));
System.out.println("size of L2= " + l2.size());
for(int i = 0 ; i<l2.size();i++){
System.out.println("\n Index is : " + i + "Content-desc is : " + l2.get(i).getAttribute("name"));
}
Modify the loop as per your needs
I'm new to Selenium webdriver. I came across a requirement where I have to run my test which clicks on all links with in a section. Can someone help me with the Java code for this. Attached a image which shows firebug properties of that particular section.
I have tried the below code but it returns me a null list.
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("https://dev.www.tycois.com/");
driver.manage().window().maximize();
List<WebElement> allElements = driver.findElements(By.xpath("html/body/div[10]/div/div[1]/div[3]/ul[1]/li[5]"));
System.out.println(allElements);
for (WebElement element: allElements) {
System.out.println(element.getText());
element.click();
}
}
Thanks in advance.
The details aren't clear but it looks like you are trying to print the links in the INDUSTRIES section of the footer. If that's true, this should work.
driver.get("https://dev.www.tycois.com/");
WebElement industries = driver.findElement(By.cssSelector("div.columns.three.alpha > ul"));
List<WebElement> links = industries.findElements(By.tagName("li"));
for (int i = 1; i < links.size(); i++)
{
System.out.println(links.get(i).getText());
}
You can't click the links in this loop because once you click the first one, you will no longer be on the page. I would suggest that you store the URLs from each link in an array and then driver.get(url) for each one. Or you could store the expected URLs in an array and compare the URLs from the links to the expected URLs and not have to navigate at all. The choice is up to you...
The solution from JeffC works with the tweak detailed below -
driver.get("https://dev.www.tycois.com/");
WebElement industries =
driver.findElement(By.cssSelector("div.columns.three.alpha > ul"));
List<WebElement> links = industries.findElements(By.tagName("li"));
for (int i = 0; i < links.size(); i++)
{
System.out.println(links.get(i).getText());
}
The alternate answer above, which I cannot comment on because I'm new to the site had
for(int i=1; i < links.size(); i++)
However this misses off the first element in the list. The suggested fix to use -
for(int i=1; i <= links.size(); i++)
will cause an IndexOutOfBoundsException.
To fix, simply set your iterator to start at 0 instead of 1
You could use like for example:
driver.findElements(By.xpath("//li[contains(#class,'managed-services')]/ul/li/a"));
It is usually bad idea to use XPath attached to root of html i.e Absolute xpath, you should always try to use the shortest selectors possible i.e Relative xpath.
Also remember that if links are hidden, you need to trigger action, which enables them - like open menu for instance.
You can try with the below code.
Just change the xpath according to your application.
List<WebElement> liElements = driver.findElements(By.xpath(".//*[#id='fk-mainhead-id']/div[2]/div/div/ul/li"));
System.out.println(liElements);
for(int i=1; i <= liElements.size(); i++)
{
WebElement linkElement = driver.findElement(By.xpath(".//*[#id='fk-mainhead-id']/div[2]/div/div/ul/li[" + i + "]/a"));
System.out.println(linkElement.getText());
}
WebElement industries = driver.findElement(obj.getElement("history_list"));
List<WebElement> links = industries.findElements(By.tagName("li"));
boolean a = false;
Thread.sleep(10000);
for (WebElement option : links) {
System.out.println("value =" + option.getText());
String s = option.getText();
if (s.equals(new_site_name)) {
a = true;
break;
} else {
continue;
}
}
I want to append div inside <TH> in GWT
Element tr = DOM.createTR();
DOM.appendChild(thead, tr);
for (int index = 0; index < headers.size(); index++) {
Element th = DOM.createTH();
Element div = DOM.createDiv();
DOM.appendChild(th, div); // this is not working !
DOM.appendChild(tr, th);
}
Is any thing wrong in my code?
I assume it will give you compile time error because the method `DOM.appendChild(th, div);' second parameter should be part of package structure:
com.google.gwt.user.client.Element
instead of
com.google.gwt.dom.client.Element