How to get text from a primefaces component with selenium web driver - java

I'm automating a system made in Primefaces and I'm having trouble finding elements.
Case 1.
I want to get the text of this span that is inside the li.
I want to get the text circled in red
I'm using this code. but I get the element not found error.
String textoErro = driver.findElement(By.xpath("//*[#id=\"messagesCargoFuncaoCU\"]/div/ul/li[1]/span")).getText();
Case 2.
I want to get the contents of the td.
I want to get the text circled in red. "AutomacaoTeste"
I'm using this code.
But I get the error: Unable to locate element.
String textoName = driver.findElement(By.xpath("//*[#id=\"dataTableAgfolhaCargoFuncao_data\"]/tr/td[3]/span")).getText();

Related

Java Selenium - POM - html xpath - no such element : Unable to locate element

I'm getting this error while trying to find the web element for testing the facebook "create a page">"sign up" button under Page Object Model. I tried various options like by class name(CSS selector) and also by copying the system xpath but getting the same error message
org.openqa.selenium.NoSuchElementException: no such element : Unable to locate element
/driver.findElement(By.xpath("//*[#id='blueBarDOMInspector']/div/div[1]/div/div/span/a")).click();
driver.findElement(By.className("_42ft _4jy0 signup_btn _4jy4 _4jy2 selected _51sy"));
Looks like you class name generates randomly - you can check it by re-visiting same page from different browsers (or you can try incognito). If it's true you need another way to work with element. Some examples:
If this Sign Up button - use xPath and look for the text in that element
Think if your task can be resolved without using interaction with UI (or part of the task). Read about facebook API and if possible use this instead of UI. It will save you a lot of time and hassle down the road.

How to find the highlighted text using selenium web driver in Java?

Hello I have tried the following link.
It is working only if we select the text manually.
But my case is like, we have found the text using the text search.
Is it possible to find the selected text using selenium web driver??
We can do this by using getCssValue method available in WebElement.
Demo:
WebElement branch = driver.findElement(By.xpath(".//h3[contains(text(),'locator')]"));
String background = branch.getCssValue("background");
Assert.assertEquals(background, "We know the actual bg color");

How to get the text from the auto suggestion of the auto complete text box?

My requirement is I want to select the particular name from the auto-suggestion in the autocomplete text box.
So here I only know how to get the name by using the mouse down to achieve this But I know it's not a good solution to get that because we don't give the guarantee to the auto-suggestion is same as all the time in the browser.
So if anyone knows how to get the auto-suggested text names for the auto-complete text box in Selenium Web Driver using Junit (Here I am using the Junit in Selenium WebDriver to develop the automation test script).
My code:
driver.findElement("//input[#id='phSearchInput']").SendKeys(KEYS.ARROW_DOWN);
Thread.sleep(1000);
driver.findElement("//input[#id='phSearchInput']").SendKeys(KEYS.ARROW_DOWN);
Thread.sleep(1000);
driver.findElement("//input[#id='phSearchInput']").SendKeys(KEYS.ENTER);
Here the above code is only working for my correct option is shows as
the second option of the auto-suggested texts.
So that's why I need how to get the text names in the auto-suggestion for the autocomplete text box.
Please the give the solutions as the JUnit Form to my question because I am using the JUnit to develop the automation test script.
Thanks
The auto-suggest box is HTML like anything else on the page. Create a locator that finds the auto-suggest panel and then parse the contents. Once you figure out the structure, you can get the text that you need and then assert that it is correct.
I can't see the full HTML from your screenshot but you can see that the list is contained in an HTML UL. The UL is the container and each LI is a list item in the dropdown. You can use that info to do something like
ul.autoCompleteGroup > li
to get all the list items. I can't see what's inside of there but at some point you should be able to use .getText() to get the auto suggest items. From there you just compare what you pulled off the list to what you are expecting with an Assert.
Please try below code
public void selectAutoCompleteItem(String itemText) {
String xpathPattern = "//div[#id='phSearchInput_autoCompleteBoxId']" +
"//ul/li/a[.='%s')]";
String xpathExp = String.format(xpathPattern, itemText);
driver.findElement(By.xpath(xpathExp)).click();
}
In that case you can use the findelements functionality. So you can say:
List<WebElement> elements = driver.findElements(by.xpath("//a[#class='autoCompleteRowLin‌​k']");
And then for
each list item you can use getText to get the exact text. Then you can assert it with the expected values

I want to perform click action on button present under "svg" layout using selenium library

I want to perform click action on a button present under .svg layout using Selenium (Java bindings).
For example, I want to click on the button element, but every time I try to find an element by xpath, I get exception `enable to locate element
I read that with Selenium its tricky to click on element present under the .svg.
Is there anybody who knows a solution because, I haven't found a suitable solution on the net by myself.
Find below HTML code look likes this way:
My code:
List <WebElement> frame1=driver.findElements(By.xpath("//iframe[contains(#id,'-06636000002Pb2L')]"));
System.out.println(frame1.size());
System.out.println(frame1.get(0).getAttribute("title"));
driver.switchTo().frame(0);
ElementaryOperations.Sleep(3000);
System.out.println("New relation frame found");
driver.findElement(By.cssSelector("#newrel")).click();
After switching to frame successfully, I am not able to click on the element present under the SVG layout. Please refer to the attached screen shot(link)
this is how you should be able to identify SVG node and then sub nodes of that.
//*[local-name() = 'svg']
use this code
driver.findElement(By.id("rfb")).click();
try using class attribute of the button using CSS Selectors:
driver.findElement(By.cssSelector(".btn.btn-primary")).click()

findElement by class where content equals text

I'm trying to locate and click an element on my page but can't use the by.id method as the id's are generated and change per session. For most elements I can get around this by using xpath but there is a dropdown menu where this does not work. I can click the element containing the dropdown and it shows me the options. If I locate the element I need and copy it's xpath the test case won't function stating it can't find the xpath. Now next to the id the Element I'm trying to click also has a class. Problem is that this class is not unique, all menu items in the dropdown have the same class with a different text. What I would like to do is something like:
driver.findeElement(By.class("x-menu-item-text").equals("Unique text 1here").click()
The class "x-menu-item-text" is not unique but the text in this particular class is. I can't use the ID as this is automatically generated. The full code for the item or element I want to click is:
<a id="ext-comp-1035" class="x-menu-item" hidefocus="true" unselectable="on" href="#"><span id="ext-gen250" class="x-menu-item-text">Unique text 1 here</span></a>
<a id="ext-comp-1035" class="x-menu-item" hidefocus="true" unselectable="on" href="#"><span id="ext-gen250" class="x-menu-item-text">Unique text 2 here</span></a>
I'm using Selenium Webdriver with Eclipse (Java).
Allthough the answer provided seems to work on most pages and locations, there is a situation however where I can't get it to work. Can anyone advise?
There is a page with buttons and I want to click one of these buttons. If I use the following statement:
driver.findElement(By.xpath("//*[#class=' x-btn-text' and text()='Add']")).click();
I get an error message
org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with
If I look at the source I see:
<button class=" x-btn-text" id="ext-gen539" type="button">Add</button>
So the element is present and visible.
I've tried adding a wait.until statement before the click statement but this does not work either:
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[#class=' x-btn-text' and text()='Toevoegen']")));
driver.findElement(By.xpath("//*[#class=' x-btn-text' and text()='Toevoegen']")).click();
Extra information: could this problem be because I'm looking for an element that is located in a popup?
You can use xpath locator
https://newcircle.com/bookshelf/selenium_tutorial/locators
By.xpath("//span[#class='x-menu-item-text' and text()='Unique text 1here']")

Categories

Resources