Select input from form - changes every submit - java

Add image of the html. I'm not able to copy multiple lines in inspect element for some reason
I'm trying to fill the input of this form
<div class="field" xpath="1"><label class="label">E-Mail *</label> <div class="control is-clearfix"><input type="text" autocomplete="on" class="input"> <!----> <!----> <!----></div> <!----></div>
My current xpath
"/html/body/div[5]/div[#class='animation-content modal-content']/div/section//section[#class='tab-content']/div[2]/div[1]/div/input[#type='text']"
The problem is the xpath I'm using changes every submission so I can only submit once. If someone can provide a xpath that doesn't change every submission I would really appreciate it!

according to the information you provided, you can Try to use this xpath:
//div[contains(#class, "field")]//input[#class="input"]
to find name:
//label[contains(text(), "Name")]/following-sibling::div//input[#class="input"]
email:
//label[contains(text(), "E-Mail")]/following-sibling::div//input[#class="input"]
phone:
//label[contains(text(), "Phone")]/following-sibling::div//input[#class="input"]

The Xpath added by the Roman is satisfactory for your needs it just you need to understand how you can improve your Xpath there are multiple ways to do it ,
I will add some more Xpath so that it will be helpful in the near future.
I personally prefer the below mentioned way of writing the Xpath
//label[contains(text(), "Name")]/following-sibling::div//input[#class="input"]
But there are some other ways to I will add one of the Xpath from my project where you can also learn how we use the parent and following-sibling
//label[contains(text(),'PlantCode*')]//parent::div[#class='rb_Work_FieldContainer']//following-sibling::div[contains(#class,'rb_Work_FieldValueArea rb_Work_FieldValueArea_create ')]//textarea[#class='textarea']
These are some of the ways to find the Xpath, You can also use the extension like chropath in the Chrome to help you out in building the Xpath,

Related

Selenium - Find children containing classes

I cannot find any regular expression to make it work.
What I need to archive
In a herarchy, I would like to get all the children (including several levels) that contains some specific classes.
For example: I would like the WebElements with classes "black" or "white"
<div class="initial-div">
<div class="red">
<div class="white">Hello</div>
</div>
<div class="black">Goodbye</div>
</div>
It should be able to find both the "Hello" and "Goodbye" divs, as they are both children.
My approach
I am trying to do it using Selenium and searching by the xPath. My expression looks like:
List<WebElement> nodes = initialNode.findElements(By.xpath("//*[#class='black' or #class='white']"));
But I am getting all the time "Invalid Expression exceptions" or no results.
Could someone give me a hand with this?
Thank you in advance!
SOLVED!
It was more complicated that I though, but I finally make it. I share with you, in case that someone is searching for this at any time:
List<WebElement> nodes = initialNode.findElements(By.xpath("*//descendant::div[contains(#class, 'black') or contains(#class, 'white)]"));
List<WebElement> nodes = initialNode.findElements(By.xpath("*//descendant::div[contains(#class, 'black') or contains(#class, 'white)]"));
If the element has two xpath, then you can write two xpaths like below:
xpath1 | xpath2
Eg:
//div[#class="black"] | //div[#class=“white"]
It will choose any one xpath

How to click on a link with a span tag inside using selenium java

I am having a problem in clicking the link text given inside a span tag.
html code :
<div id="menu" style="width: 1752px;">
<div class="dd_menu" dd_event_id="dd_event_2">
<a class="dd_menu_menu_entry dd_menu_entry_clickable" href="javascript:void(0);" style="left: 3px; width: 111px;" dd_menu_id="0">
<a class="dd_menu_entry dd_menu_entry_clickable" href="javascript:void(0);" style="left: 114px; width: 131px;" dd_menu_id="1">
<span class="text" style="background-color: rgba(0, 0, 0, 0);">FirstMenu</span>
I need to click on the text 'FirstMenu' .
I have used the xpath : .//*[#id='menu']/div/a[2]/span
It does not seem to work. How do I fix it?
If your requirement is to "click on the link FirstMenu", then you should use that as the locator. No need to mess around with XPath.
driver.findElement(By.partialLinkText("FirstMenu")).click();
The .partialLinkText() locator strategy should account for any extra whitespace padding due to the extra span element.
Your xPath returns span element so you're clicking that span. To make your xpath return a link ament your query to the following:
//*[#id='menu']/div/a[span]
This query returns a "link" that has span element as a child.
Try to use below xpath :-
//span[contains(.,'FirstMenu')]
If it doesn't work then there may be any frame present. You need to switch it on first.
Please let me know if there is more element with name FirstMenu on DOM
Hope it will help you :)
The problem got solved by using the same xpath i specified above with the usual syntax driver.findElement(By.xpath("//*[#id='menu']/div/a[2]/span")).click(); after i gave the order by which testcases have to be executed and using #Test(priority=something) and giving some implicit waits.
Thank you all for the suggestions.
regards,
roma
It is not good to use xpath. If the html of the page is changed your code would stop working. Try with css selector.
This is is simple code and you can modify it for you case:
var collection = driver.getelementsBy(By.cssSelector('div#menu div'))
It should return you collection with elements
And after that you can iterate through collection and find the element you want to click.
Hope the answer helps you.

Finding element with dynamic xpath not working

I have this in the HTML:
<textarea name="comment" class="form-control" rows="3" id="textarea_1160688690910416779_2159935466"></textarea>
I want to interact with id=textarea_, but the numbers are constantly changing after the "_". To solve this, I used this code:
driver.findElement(By.xpath("[starts-with(#id, 'textarea')")).sendKeys(comment);
However I am getting the error:
org.openqa.selenium.NoSuchElementException: Unable to locate element:
Use
driver.findElement(By.xpath("//textarea[starts-with(#id,'textarea_')]")).sendKeys(comment);
The reason you're getting this error is that you need to add '//' before your current XPath.
You could either use starts-with, this way:
("//textarea[starts-with(#id, 'textarea_')]")
You could also give 'contains' a try:
("//textarea[contains(#id, 'textarea_')]")
In both ways, you could use //*[... instead of textarea, for more cases
Your Xpath is not a valid Xpath.
It should contain a path and tag name
and it should be balanced (your [ is not closed)
So, if your tag is, textarea, and if it is at top: use /textarea
if not at top, use //textarea
This gives: "//textarea[starts-with(#id, 'textarea')]"
To use with selenium, you could also read that: JAVA - How to use xpath in selenium
and that: Webdriver findElements By xpath

Handle elements that have changing ids

I am running the script to automate test cases and found that id's keep on changing. Below is my HTML code
Firebug for test drive:
<button class="G0036HC-b-a G0036HC-b-o G0036HC-b-g" id="gwt-uid-470" tabindex="0" aria-labelledby="gwt-uid-470" role="button" type="button"><div class="G0036HC-b-j">Click to continue</div></button>
Inspector:
<button class="G0036HC-b-a G0036HC-b-o G0036HC-b-g" id="gwt-uid-320" tabindex="0" aria-labelledby="gwt-uid-320" role="button" type="button"><div class="G0036HC-b-j">Click to continue</div></button>
Only id's changes.
Any Help would be appreciated.
You can get the element by xpath and check that id attribute starts with gwt-uid-:
//button[starts-with(#id, "gwt-uid-")]
Besides, the answer #alecxe suggested I would also suggest you to try with text based xpath search. I often faced issue with wait so also suggest to use explicit wait this.
// //div[.='Click to continue']/..
By byXpath = By.xpath("//div[.='Click to continue']");
WebElement myDynamicElement = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(byXpath));
// myDynamicElement.click();
Considering the following observations, the below code should work fine.
All the buttons on the page are prefixed with gwt-uid- with changing number suffix
If the buttons on the page have a unique text then use
By.xpath("//button/div[text()='Click to continue']")
If there are multiple buttons with same text then use indexes. eg. two close buttons with same text then use,
By.xpath("(//button/div[text()='Close'])[2]") # for the 2nd occurence, mind that selenium is not zero indexed
use following...
//input[#id=’’]/following::input[1]
You can use preceding in the same way to identify the child node

Select invalid html-tag with Selenium

I am trying to get a WebElement with Selenium:
driver.findElement(By.xpath("//input[#name='j_username']"))
But Selenium says: "Unable to find element with XPath ...".
The XPath is valid, I proofed it with FirePath.
But the input element has the following invalid code:
<input size="10" type="text" name="j_username" maxlength="8">
I can't change the html-file, despite the fact is there any solution to get the webElement?
Thanks in advance!
try select element with css selector. and also verify in firepath(firebug addon that element is located properly).
so your css selector be something like
input[name='j_username']
2nd approach is to use internal firebug mechanism for finding xPaths of elements.
See screen attached below
After these manipulations driver shoulda handle element properly.
Well I will suggest adding an id to your html code -
<input id="j_username"size="10" type="text" name="j_username" maxlength="8">
and findElement by id -
driver.findElement(By.id("j_username"));
I have faced similar issues with xpath(borwser issues??) but id never fails for me. ;)
By the way I feel your code should be -
driver.findElement(By.xpath(".//*[#name='j_username']"));
The best solution is to find out what selenium is doing wrong, but without a URL or sample page to test on it's a little hard. Is there anyway you could dump the HTML into a jsfiddle? If there is do that and paste the url into the question and I'm sure someone can find a solution.
If not however, another way to get the results is to do it with jQuery. If firebug is picking it up but not selenium, then there's no reason why jQuery wouldn't get it. Here's how to go about doing that if needed:
Step 1: Is jQuery already present on the page? If so then you don't need to do this bit, otherwise you will need to add it yourself by using driver.executeScript(addjQueryScript) where the script does something like this.
Step 2: call WebElement input = driver.executeScript(elementSelector); where the elementSelector script would be something like \"return $('input[name=\"j_username\"]')\");
My jQuery's not so good, but I believe that should work...
Best of luck!

Categories

Resources