I like to select this tag in the page using selenium in java
<input class="btn btn-success addReportBtn" type="submit" />
here is what I've tried so far:
driver.findElement(By.xpath("//input[type=submit]"));
driver.findElement(By.cssSelector("//input[#type='submit']"));
I get these exception for both of them respectively:
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"input[type=submit]"}
org.openqa.selenium.InvalidSelectorException: invalid selector: An invalid or illegal selector was specified
if I use Jsoup I can easily get it by doing:
System.out.println(document.select("input[type=submit]"));
what am I doing wrong?
As pointed out by #Jason, you are seriously mixing up XPath expression and CSS selector syntaxes. Correct expressions would be:
driver.findElement(By.xpath("//input[#type='submit']"));
driver.findElement(By.cssSelector("input[type=submit]"));
Note that I would not only check the button type since usually there are multiple submit buttons on a page. There is that addReportBtn class I would rely the locator on:
driver.findElement(By.cssSelector("input.addReportBtn"));
And that also gives a plus one to readability.
Try these:
driver.findElement(By.xpath("//input[#type='submit']"));
driver.findElement(By.cssSelector("input[type='submit']"));
Related
I am having trouble in selecting a selector when I am trying to select it as a 'css-selector'
I have this selector:
<div role="button" class="jss300 jss299" tabindex="-1">
<span class="jss313">system-all</span></div>
</div>
and I am trying to get the css-selector from it, I tried this way:
"div[class~='system-paloaltonetworks']"
and my need is to get the text from the selector, in this case I want to get "system-paloaltonetworks" into string variable.
hope now the question is clear.
"system-paloaltonetworks" is the element text, not the class attribute (the class is jss313). You can't locate it with cssSelector you need to use xpath (you should also notice the element has span tag, not div)
driver.findElement(By.xpath("//span[text()='system-paloaltonetworks']"));
You are using class~= but aren't comparing with the class...
You try: driver.findElement(By.xpath("//*[#class='jss313']"));
I would like to retrieve a webelement out of a nested html path using either css selectors or xpath. My specific use case is that I would like to select the i element in the following snippet:
<td class="headerActionsTd" data-rolename="Speaker">
<div class="headerActions">
<span class="addNewParticipantSection">
<i class="icon fa fa-user-plus" title="Add New"></i>
</span>
How do I obtain the i webelement for this using either css selector or xpath?
Use this xpath: //td[#data-rolename='Speaker']//div//span//i[#title='Add New'] or
css : div.headerActions i
driver.findElement(By.cssSelector("div.headerActions i"));
for multiple elements :
List<WebElement> users = driver.findElements(By.xpath("//td[#data-rolename='Speaker']//div//span//i[#title='Add New']"));
A simple one would be this :
CSS_SELECTOR
i.icon.fa.fa-user-plus[title='Add New']
Note that, if there is multiple element with this css selector, then you have this facility to differentiate between them:
:first-child
:nth-child(n)
:nth-last-child(n)
More can be found at this link
XPATH : would be :
//td[#data-rolename='Speaker']/descendant::span[#class='addNewParticipantSection']/i
Hope that helps.
Since you said data-role only changes in the comment of the other person, here is the xpath you can use
"//td[#data-role='Speaker']//i"
Or
"//td[#data-role='Speaker']/div/span/i"
Another option is that you can attempt to locate elements inside other elements. For example, you could store the addNewParticipantSection div in a WebElement, then use that WebElement's findElement method to locate the i element within it. Like so:
WebElement section = driver.findElement(By.className("addNewParticipantSection"));
WebElement icon = section.findElement(By.tagName("i"));
icon.click();
I am trying to find an element on a website using Selenium. The page I am looking at is:
http://www.usaswimming.org/DesktopDefault.aspx?TabId=1470&Alias=Rainbow&Lang=en-US
Specifically I am trying to find the element for the "Last Name" input box and send keys in Java. the html looks like this for the text box:
<div class="field">
<input id="ctl62_txtSearchLastName" type="text" maxlength="36" name="ctl00$ctl62$txtSearchLastName">
</div>
Therefore, I initially attempted to get the element through the id which is unique:
WebDriver driver = new InternetExplorerDriver();
driver.get(timeSearchSite);
...
driver.findElement(By.id("ctl62_txtSearchLastName")).sendKeys(lastName);
However this generated the following error:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to find element with id == ctl62_txtSearchLastName (WARNING: The server did not provide any stacktrace information)
I also tried to use the name which failed similarly. Since this is a javascipt page I thought that I might need to wait before trying to access the element. I tried an explicit and an implicit wait and both resulted in timeouts. My next thought was that I was in the wrong frame but I cannot seem to find another frame. I tried indexing since I don't have names but I couldn't locate any other frames. There is this at the top of the page which might be messing selenium up:
<noscript><iframe src="//www.googletagmanager.com/ns.html?id=GTM-WMWM93" height="0" width="0"
style="display: none; visibility: hidden"></iframe></noscript>
What could be causing this error? How can I locate the element? Any help would be greatly appreciated/
Using an explicit wait worked for me (tried both chrome and firefox):
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(
ExpectedConditions.visibilityOfElementLocated(By.id("ctl62_txtSearchLastName"))
);
element.sendKeys("test");
Resulted into:
I am new to selenium and trying to autoamte a web application in junit framework. As many get some problem in identifying web elements,I too stuck at a point where two submit buttons are having same xpath and css selector.
The only difference what I can observe is.. In the two form tags, I can see that className is different(for first form tag it is "feature_space_checkbox" and for second form tag it is "auto_fs_steps_checkbox")
As, I need to identify the second submit button..So I tried to identify the second submit button as below
driver.findElement(new ByChained(By.className("auto_fs_steps_checkbox"),By.xpath("//*[#id='edit_brochure_2863']/input[3]")));
When I try to execute this, I got the error as
org.openqa.selenium.NoSuchElementException: Cannot locate an element using By.chained({By.className: auto_fs_steps_checkbox,By.xpath: //*[#id='edit_brochure_2863']/input[3]})
Can anyone please correct me where I made the mistake
Adding DOM for this scenario
<form action="/brochures/2865/feature_space_checked" class="feature_space_checkbox" id="edit_brochure_2865" method="post"><div style="margin:0;padding:0">
<input name="commit" type="submit" value="Submit">
</form>
For the second submit button it is..
<form action="/brochures/2865/update_auto_fs_steps" class="auto_fs_steps_checkbox" id="edit_brochure_2865" method="post"><div style="margin:0;padding:0">
<input name="commit" type="submit" value="Submit">
</form>
Firstly, XPath and CSS selectors are not definitive. There are many XPath and CSS for every element on a page so to say they have the same Xpath and CSS selectors is incorrect.
For your example, is there any need to use XPath or combine two selectors?
The following CSS would work;
form.auto_fs_steps_checkbox input
There is no need to use chaining as this can all be expressed in XPath:
//*[#id='edit_brochure_2863' and #class='feature_space_checkbox']/input
So this will be in Java:
driver.findElement(By.xpath("//*[#id='edit_brochure_2863' and #class='feature_space_checkbox']/input"));
Of course, for the second submit button it will be
driver.findElement(By.xpath("//*[#id='edit_brochure_2863' and #class='auto_fs_steps_checkbox']/input"));
xpath for the second submit would be
driver.findElement(By.xpath("//form[#class='auto_fs_steps_checkbox']/input"));
This is enough to identify the second button as here class name is unique and id is same for both. So its better we do it by class name .
I am trying to get the text content of the items inside the following 2 div elements but getting an error that the element cannot be found. For example the text content for first div is "Text1". How can I get that text?
So far, I have tried:
driver.findElement(By.xpath("//*[#id='ctrlNotesWindow']/div[3]/ul/div[1]/div[2]/div[2]")).getText())
and that complains of not finding that element.
Here is the html code:
<div class="notesData">
<div class="notesDate" data-bind="text: $.format('{0} - {1}', moment(Date).format('MMM DD, YYYY h:mm a'), User)">Text1</div>
<div class="notesText" data-bind="text: Note">Text2 on next line</div>
</div>
Here is the error, I get:
Exception in thread "main" org.openqa.selenium.NoSuchElementException:
Unable to locate element:
{"method":"xpath","selector":"//*[#id='ctrlNotesWindow']/div[3]/ul/div[1]/div[2]/div[2]"}
Don't use meaningless XPath. Your error message tells you "NoSuchElement", so you had your locator wrong, which has nothing to do with getText (not yet). Try using CssSelector or meaningful XPath:
driver.findElement(By.cssSelector("#ctrlNotesWindow .notesData > .notesDate")).getText();
If for whatever reason you'd want to stick with Xpath:
driver.findElement(By.xpath("//div[#class='notesData']/div[#class='notesDate']")).getText();
Otherwise, the css selector answer above is a good option.
Another alternative is to locate the element by class name:
driver.findElement(By.className("notesDate")).getText();
It works with:
driver.findElement(By.xpath("//div[#class='ui-pnotify-text']")).getText();
which is an PNotify notification.