Well not to mention that I'm new on using the selenium web driver, I'm trying to automatize a mailbox so I can log myself in and to send a message, the thing is that I download the firebug plugin for Mozilla, that is the browser I'm currently working with, when I'm trying to find the element for the log in and the password everything goes well, because I do it by their name.
input class="_nb-input-controller" type="text" **name="login"** autocorrect="off" autocapitalize="off" value=""
input class="_nb-input-controller" type="password" **name="passwd"** value=""
but as soon as im at the mailbox entry the identificator changes it, i go to compose link and this is the code that shows up:
a class="b-toolbar__item b-toolbar__item_compose js-toolbar-item-compose daria-action" title="Compose (w, c)" href="#compose" data-action="compose.go" data-params="toolbar=1&toolbar.button=compose"
Since I don't find any method that could help on this, a friend came and told me about the xpath which was:
/html/body/div[2]/div/div[5]/div/div[3]/div/div[2]/div/div/div/div[2]/a[2]
by doing this selenium did find the element and I could click on the icon,
but again.. when I'm trying to get into the "TO" prompt so I can write inside
it is not finding the element, not by xpath, or anything is there a way or formula to get elements or translate them into a way that selenium can find them???
oh i see, i re try with the element and i noticed that the one i wanted was showing as hidden using firebug so i just try the next one up and the web driver could locate the item, it was a simple app to send an email the prompt that i wanted to locate was the 'TO' field. and thanks guys for giving me the correct way of using the Xpath property
Having locators such as :
/html/body/div[2]/div/div[5]/div/div[3]/div/div[2]/div/div/div/div[2]/a[2]
is generally a bad idea. If you want to build a stable test I would suggest you stay away from such.
Now I see you have attributes like #name and #class. XPath is provides you with a way to reach these elements directly by pointing the identifiers instead of the whole path to the element.
You can try something like:
//input[#name='login']
//input[#name='password']
More info at: http://www.w3schools.com/xsl/xpath_intro.asp
Related
<div class="jss7113 jss7118"><div></div><div class="jss7114">Invalid username or password</div><button class="MuiButtonBase-root MuiButton-root MuiButton-text MuiButton-textPrimary" tabindex="0" type="button"><span class="MuiButton-label"><i icon="close-circle-outline" title="" class="mdi mdi-close-circle-outline" aria-hidden="true"></i></span><span class="MuiTouchRipple-root"></span></button></div>
This is what wrote
//System.out.println(driver.findElement(By.cssSelector("*.jss5107")).getText()); //Get the error message from the Invalid credentials
//System.out.println(driver.findElement(By.cssSelector("div[class = 'jss7114']")).getText().equals("Invalid username or password"));
//System.out.println(driver.findElement(By.xpath("//div[#class = 'jss7114']")).getText().equals("Invalid username or password"));
It seems like the class name you're trying to find doesn't exist:
//System.out.println(driver.findElement(By.cssSelector("*.jss5107")).getText());
this type of gibberish names for elements usually indicates random attribute values being generated and making your automation relying on those values is a no go because we want our automation to be as solid and low maintenance as possible.
Instead, try to find a better way to locate the element you're looking for.
I could've tried to give you a better locator but it seems like you pasted a part of the HTML code which I can't read it properly (for example there's a div tag that immediately gets closed right after):
<div class="jss7113 jss7118"><div></div>
Please provide a better snippet of this part of the HTML in order for us to help with your question.
If you want to validate the message, you can locate the element by the text within it:
driver.findElement(By.xpath("//div[contains(text(),'Invalid username or password')]
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.
I'm using Selenium Web Driver for automation and with this new app, I'm not able to find element by normal method.
My code looks like:
driver.findElement(By.xpath("html/body/div[1]/md-sidenav/md-list[1]/md-list-item[2]/button")).click();
And I want to click on 'Views'. Need some help, I have tried following methods for xpath:
//span[contains(#class, 'ng-binding ng-scope') and normalize-space()='Data']
//div[1]/md-sidenav/descendant::span[text()='Data']/button ---- no success
When I'm using :
driver.findElement(By.xpath("html/body/div[1]/md-sidenav/md-list[1]/md-list-item[2]/button")).click();
Above code works but I don't want to use item[x], because they will changes very often.
Please help!
Thanks!
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!
How can I access this element:
<input type="submit" value="Save as XML" onclick="some code goes here">
More info: I have to access programmatically a web page and simulate clicking on a button on it, which then will generate a xml file which I hope to be able to save on the local machine.
I am trying to do so by using HtmlUnit libraries, but all examples I could find use getElementById() or getElementByName() methods. Unfortunately, this exact element doesn't have a name or Id, so I failed miserably. I supposed then that the thing I have to do is use the getByXPath() method but I got completely lost into XPath documentation(this matter is all new to me).
I have been stuck on this for a couple of hours so I really need all the help I can get.
Thanks in advance.
There are several options for an XPATH to select that input element.
Below is one option, which looks throughout the document for an input element that has an attribute named type with the value "submit" and an attribute named value with the value "Save as XML".
//input[#type='submit' and #value='Save as XML']
If you could provide a little bit more structure, a more specific (and efficient) XPATH could be created. For instance, something like this might work:
/html/body//form//input[#type='submit' and #value='Save as XML']
You should be able to use the XPATH with code like this:
client = new WebClient(BrowserVersion.FIREFOX_3)
client.javaScriptEnabled = false
page = client.getPage(url)
submitButton = page.getByXPath("/html/body//form//input[#type='submit' and #value='Save as XML']")
Although I would, in most cases, recommend using XPath, if you don't know anything about it you can try the getInputByValue(String value) method. This is an example based on your question:
// Fetch the form somehow
HtmlForm form = this.page.getForms().get(0);
// Get the input by its value
System.out.println(form.getInputByValue("Save as XML").asXml());