I am not able to locate the chrome downloaded filename using locator. I need to get the filename to proceed further on automation to verify the content.
Element HTML:
<a is="action-link" id="file-link" focus-row-control="" focus-type="fileLink" tabindex="0" role="link" href="https://energyfuture.cgi.com/pasdemo/operations/QueryDisplay!displayQuery.action?QUERY_TIMESTAMP=1614571607262" class="">Facility Balance_1614571607828.xlsx</a>
Here the numbers of filename are dynamic, but the prefix Facility Balance will be constant.
code I have tried:
driver.navigate().to("chrome://downloads/");
String Text = driver.findElement(By.xpath("//*[#id='file-link']")).getText();
OR
String AttributeData = driver.findElement(By.xpath("//*[contains(text(),'Facility Balance')]")).getAttribute("linkText");
For starters
.getAttribute("linkText");
will not work because your target node does not contain an attribute called linkText.
The second point,
By.xpath("//*[#id='file-link']")
might not work if you have multiple elements with the same ID on the page (this include popups). Consider refining your xpath to be more explicit. For example, By.xpath("//a[#id='file-link' and ...]") where the ... are additional conditions that will refine your search to find only one.
Out of curiosity, did you try your Xpath directly in the browser developer's tools? Before you attempt to code things like this, you should always attempt to evaluate your XPath expressions to make sure that 1) they are accurate and 2) they are not ambiguous. Unfortunately, given the amount of information posted, this is the extent of the help I can give you.
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')]
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
I`m trying to use xpath type of parameter in my webdriver script.
However, although this parameter actually selects the same HTML tag:
By START_BUTTON = By.xpath(".//*[#name='MyName']");
as this one
By START_BUTTON = By.name("MyName");
I`m not being able to use the by.xpath in my testing script while the by.name is working fine
Not sure why I`m facing this issue. When tested in Firebug/FirePath the path:
.//*[#name='MyName'] - points exactly to the same A tag as By.name("MyName");
Thanks
My bad, I had the part of the code commented which actually caused this.
Sorry for bothering you.
Regards
Posting it for reference :
These are the locator strategies which helps to identify an element.
Following strategies are mainly used to identify an element :
By ID
By Name
By Class Name
By Tag Name
By Link Text
By Partial Link Text
By CSS
By XPath
I am trying to click on link which is generating runtime with some number at the end :
Html code is as below:-
<span class="float-left">
<a href="/Key/Lock/369">
How to use regular expression for xpath? please help.
It is working for exact no like:-
WebElement LockAndEdit = driver.findElement(By.xpath("//*[#href='/key/Lock/370']"));
I am trying below code with regular expression at last for any number but not working:-
WebElement LockAndEdit = driver.findElement(By.xpath("//*[#href='/key/Lock/\\d+']"));
Please help.
You don't need to go that far.
An XPath expression like this will work fine:
//a[starts-with(#href, '/Key/Lock')]
And even a normal CSS selector:
a[href^='/Key/Lock']
Note, this assumes it is always a number at the end, if it may not be, then it becomes much more difficult.
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());