How to find an element in new Webpage using selenium webdriver? - java

I have a form. I automated the task of filling up the form and clicking the submit button using Selenium WebDriver.
Now in the next page where the URL has changed, there is a confirmation message "Form Submitted Successfully". How do I verify this element in the new URL?
Thanks in advance!

Try to use JUnit assertion to compare expected string with actual the one you getting on the page, this should work :
String actual = driver.findElement(By.xpath("your_locator").getText();
Assert.assertEquals(actual, expected);

As per my knowledge, even if the objects you are working on are on different page(url) it does not matter, you just need to have your locator correctly written.
Using getText() method you could get text on the page. Verify whether your locator is pointing to correct text using xpath validaters or other means.
Once you have your locator handy apply getText() method on it and get the text.
Once you have text, just verify it or print it to see if it is the same.

Related

How to write xpath for this field?

enter image description here
driver.findElement(By.xpath("//input[#id='QuickSearchProduct']")).sendKeys("ddd");
I am trying to get xpath for this field but unbale to pass any text on here. what i am missing?
If the ID is unique in HTML DOM, use id not xpath.
Xpath is least preferable choice in Selenium due to not having native support in different browser.
driver.findElement(By.id("QuickSearchProduct")).sendKeys("ddd");
also put some delay to let the web element rendered fully, and then you can interact with it. Explicit waits comes handy in these cases.
You can use id directly.
driver.findElement(By.id("QuickSearchProduct").sendKeys("ddd");
Xpath:
driver.findElement(By.xPath("//input[#id='QuickSearchProduct']").sendKeys("ddd");

Can I obtain this input element with Selenium?

I'm new to Selenium.
I have been looking all the ways possible to resolve this problem (at this point I think it is just unsolvable) I have a web page (can't share) with this input:
/html/body/table/tbody/tr[2]/td[2]/table/tbody/tr[3]/td/table/tbody/tr/td[2]/table/tbody/tr[2]/td/div/iframe/#document/html/body/div[2]/div/iframe/#document/html/frameset/frame/#document/html/frameset/frameset/frameset/frame/#document/html/body/div/form/table/tbody/tr[2]/td[2]/input
(As you can see the structure has a mix of frames, iframes and framesets; some of them have id and other names some none)
The problem is that Selenium will not find the element by any method I
have tested
. First I tried with a simple driver.findElement(By.all of them)
After they didn't work I start looking on the web and I couldn't find why I couldn't get a handle of this.
It is the only input in the web page and has an id as attribute, so it should be easy.
This is the class where I have been trying to make it work, If you want to check my unsuccessful work I focused my last efforts on the attempt number 8.
As you can see I have been trying to obtain it by several ways, the frames really seemed an issue but it was not them, nor the waiting.
Is it really no way of getting this element? Is it one of those cases
where Selenium can't automate? Or it is that I'm missing something.
object IS visible and there is not even a scroll bar, the whole page fits in the screen perfectly, Xpath was one of the first choices I tested, didn't work
Thank you in advance.
I don't know if this is the problem, but is the element you trying to use visible or you need to scroll in order to view it? If it's not visible try using this to scroll a little bit :
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0,1000)");
Another possible solution is to try use the absolute xpath instead of relative. There are several tools to find the absolute xpath of an element in an html page. Then you can use
driver.findElement(By.xpath(absoluteXpath));
After a lot of try I realized that selenium was switching to a different frame at the last switchTo. This is probably a bug, but I modified the way I was getting the last switch to to instead of
var wait = (new WebDriverWait(driver, secsToWait));
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(frameNameOrId));
To
driver.switchTo().frame(driver.findElement(By.xpath("//frame[#"+attribute+"='"+frameNameOrId+"']")));
So it finally worked and obtained the input normally.

How to find the xpath of a header title in Selenium Webdriver with Java

I need to get the xpath of a header title that only provides the class and text itself within the 'inspect element' tab.
The goal is to have an if statement to check to see if the title of the page matches with my intended header so that I can run code that will command Selenium. The reason why I don't just use "find web elements by linkText" is because the actual content of the header is viewable in various pages. Therefor, my code would ALWAYS run since the text would always be present. I also cannot just use the class since the class name is the same for every page.
I need it to be specifically the header itself using BOTH the class name and text.
I have tried
if(driver.findElements(By.xpath("//*[#class='className' and contains (text(),'headerText')]")) !=null) but even if the header text doesn't match it will continue to run. I believe it would be because technically the class is present as well as the text itself although they are different elements.
Two possible points there:
driver.findElements(..) will never be null. It will return an empty collection if no matching elements found. So, use driver.findElements(..).isEmpty() to handle case when nothing was found
Try using contains(.,'headerText') instead of contains(text(),'headerText') if still no elements found. The first option will work when your header text element contains some child tags / formatting tags.

Object inside inner html not getting identified by selenium web driver

I want to select and click on the object present inside inner html (shown in the image). But the object is not getting identified. I am using Java.
Note --> my application not opening with any browser except Internet Explorer and I can't verify xpath from console/debugger so I have to verify it through code only.
Code I have tried so far but not working for me-->
option 1 -->
driver.switchTo().frame("nav");
driver.findElement(By.xpath("//a href[#text='Administrate']")).click();
option 2 -->
driver.switchTo().frame("nav");
driver.findElement(By.xpath("//a[#text='Administrate']")).click();
option 3 -->
driver.findElement(By.xpath("/html/frameset/frame[1]/html/body/ul/li/ul/li[1]")).click();
You are checking for exact text match, use contains instead
driver.findElement(By.xpath("//a[contains(text(), 'Administrate')]")).click();
Or
driver.findElement(By.xpath("//a[contains(., 'Administrate')]")).click();
Please note the difference between text() and #text
You need to change your XPath like
driver.switchTo().frame("nav");
driver.findElement(By.xpath("//a[text()='Administrate system']")).click();
Note:- Here you need to pass complete string for better learning please refer Firepath to create and evaluate your xpath
Also can use contains method to find an element on partial text basis

Getting webpage text

So I'm using Selenium for java and am trying to grab some text from a website after connecting to it. I know selenium has
driver.getPageSource();
But if there are variables it will return the variables and not the actual data, for example if the actual text I want is
13-11-28
What it actually is in the source is
/month
or something like that. How can I get the actual text using selenium or another library.
You could find the element and the perform 'getText' on it.
Example
driver.findElement(By.cssSelector(".month").getText())

Categories