How do I select in image on and click on it using Selenium web driver? Say if it says this
<style type="text/css"> <ul id="nav"> <li> <li> <li> <li> <li> My Dashboard </li> </ul>
Would I use
driver.findElement(By.linkText("My Dashboard")).click();
or something else?
If you want to click on link in your example, you can use the selector you wrote, different kinds of css selectors (for example, By.cssSelector("#nav a") (looks for a link inside the "nav" list) or By.cssSelector("a[href='dashboard.action']") (looks for a link with specific href)) or using xPath selectors.
The important thing is to have a unique identifier to locate your element and an identifier that will fire 100% of the time.
For example, if you expect the link text to change on you, then don't look for that particular link text, because you have no guarantee that it will work 100% of the time.
Similarly, if there are 30 different elements that have the same id tag, don't use that either.
If things turn out to be very complex... that is, if you are in a large page with a lot of unknown variables, find by XPATH.
In the end, it really depends on the complexity of the website you are entering, and the goal of what you need done.
For more information, go to the Selenium javadocs and click BY on the sidebar for a list of different methods and how to use them.
If you need to click a link with an image, it would be better to locate the element with the explicit wait.
Example :
new WebDriverWait(driver, timeout).until(ExpectedConditions.presenceOfElementLocated(locator));
Related
I cannot click on a specific button.
Its name is "Select file" and it should open a windows popup:
<a class="btn btn-primary btn-sm btn-upload">Select file</a>
The XPATH is:
//*[#id="div-add-file"]/a
I tried something like this but it seems not to be clicking anything and that's strange:
driver.findElement(By.xpath("//*[#id=\"div-add-file\"]/a")).click();
I also tried something like this:
driver.findElement(By.linkText("Select file")).click();
What's going on here?
First note is that if possible, always tag the element itself with the id so your selectors are trivial.
Also, CSS selectors are usually more readable for humans. Under the hood, it's all xpath in the end, so it's good to be familiar with it, but it's not optimal. It's not really possible to diagnose your issue with the information provided. One possibility is that the selector is wrong, another is that the element is not loaded yet and you need to include a wait. A third possibility is your driver, or the browser version. Selenium has many things that can potentially go wrong
If the class value is unique, then you can use:
driver.findElement(By.xpath("//*[#class='btn btn-primary btn-sm btn-upload']")).click()
(or)
driver.findElement(By.cssSelector(".btn.btn-primary.btn-sm.btn-upload")).click();
otherwise:
driver.findElement(By.xpath(".//a[contains(text(),'Select file')]")).click();
how to get the text "xxxx" and it's url using JSOUP.
<div style="width:45%;float:left;border: dashed 1px #966;margin:0 10px;padding:10px;height:400px;">
<ul>
<li>xxxx</li>
<li><b>years:</b>2015</li>
<li><b>language:</b>non </li>
<li><b>color:</b>color</li>
</ul>
</div>
This is my current approach but I receive nothing:
Elements mvYearElement = doc.select("div[style*=width:45%;float:left;border: dashed.1px #966;margin:0 10px;padding:10px;height:400px;]");
The problem is probably that styles do not need to appear in an particular order. Your selector however fixates the order and lists a lot of styles. I would try to identify the part of the style the really is discriminating the link and only use this part. Since I don't know the rest of the HTML i only could guess what is that discriminating part. This maybe?
Elements els = doc.select(div[style*=dashed]);
That is only a wild guess however. But maybe it is also the contents of the div that are discriminating it from the others? In that case you could do something like this:
Elements els = doc.select(div[style]:has(ul));
Or something else. If you would share more of the HTML I could be more specific.
I have a grid which displays some records. When I click on a record and inspect that element it is shown that it is hidden but it is visible in the grid.
My HTML is:
<a href="http://192.168.1.6/eprint_prod_3.8/settings/othercost_add.aspx?type=edit&id=805" title="Plastic Spiral Bind"
<div style="float: left; width: 99%; overflow: hidden; height: 15px; overflow: hidden"> Plastic Spiral Bind </div>
</a>
The above code is hidden while inspecting but it is visible in grid.
Selenium code:
driver.findElement(By.partialLinkText("Plastic Spiral Bind")).click();
First store that element in object, let's say element and then write following code to click on that hidden element:
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].click();", element);
You have two approaches. Selenium has been specifically written to NOT allow interaction with hidden elements. The rational is that if a person cannot perform that action, then neither should Selenium. Therefore, to perform the click via Selenium, you must perform the action a user would do to make that button visible (e.g mouse over event, click another element, etc) then perform the click once visible.
However, Selenium does allow you to execute Javascript within the context of an element, so you could write Javascript to perform the click event even if it is hidden.
My preference is to always try and perform the actions to make the button visible
Here is the script in Python.
You cannot click on elements in selenium that are hidden. However, you can execute JavaScript to click on the hidden element for you.
element = driver.find_element_by_id(buttonID)
driver.execute_script("$(arguments[0]).click();", element)
overflow:hidden
does not always mean that the element is hidden or non existent in the DOM, it means that the overflowing chars that do not fit in the element are being trimmed. Basically it means that do not show scrollbar even if it should be showed, so in your case the link with text
Plastic Spiral Bind
could possibly be shown as "Plastic Spir..." or similar. So it is possible, that this linkText indeed is non existent.
So you can probably try:
driver.findElement(By.partialLinkText("Plastic ")).click();
or xpath:
//a[contains(#title, \"Plastic Spiral Bind\")]
I did it with jQuery:
page.execute_script %Q{ $('#some_id').prop('checked', true) }
If the <div> has id or name then you can use find_element_by_id or find_element_by_name
You can also try with class name, css and xpath
find_element_by_class_name
find_element_by_css_selector
find_element_by_xpath
Use an XPath of the link by using Selenium IDE to click the element:
driver.findelement(By.xpath("//a[contains(#title, \"Plastic Spiral Bind\")]")).click();
Is there a way to VIEW the HTML source code that GWT produces? Currently I just give my flex table the DIV id and that DIV is all HTML I can see in ViewSource.
Is there a way to structure my table in HTML (say using div's and lists) and than create a something like FlexTable around that?
To answer the original question, you can view the HTML GWT has rendered via 'Inspect Element' in Firefox, with Firebug is installed. Alternatively the Web Inspector in Safari/Chrome will do the trick, as will the Developer tools in both IE8 and Opera.
Well well it seems the answer is in the documentation.
In particular Organizing Projects outlines how we can bind different widgets to different id's on the page.
So I can effectively do something like:
# html
<div id="id_table"></div>
<div id="id_next_button"></div>
# java
t = new FlexTable()
RootPanel.get("id_table").add(t);
nextbtn = new Button("next");
RootPanel.get("id_next_button").add(nextbtn);
Wohoo!
Regarding the second part of your quetion. It is possible to create a HTML component in GWT. The recomended way to do this is extending ComplexPanel and create the elements using Document.get().createXXXElement(). But it is a little laborius.
Check out this dicussion and I am sure there are other articles about this around the internet. You can also study the code of other components the extend ComplexPanel.
So, I was trying out a test case on a website and Selenium registers the events perfectly. Now, if I have to search for a particular class and get the innerHTML of that class, how would I do it assuming that I am using Java as the driving language?
Just to be more clear. I have a class like this:
<h1 class="classname">.....</h1>
I want to get the entire text between those tags.
And finally, if the ids of the buttons on the page are dynamically generated, how would I test the clicking action on them? These button, I presume are using ajax. When I click on the button, Selenium generates this:
//div[#id='c4aef94de622f51859827294']/div/div/div[1]/span[2]/span/span[3]/a
and the actual HTML of the button is this:
<a href="#" bindpoint="forward" class="ButtonForward"/>
Is it even possible to click on the button?
You could use the getHtmlSource command to get the entire HTML source, and then use something else to parse the HTML in Java and extract the contents of the target element.
You can locate elements without using an ID, for example if this is the only such button on the page you could locate it:
Using CSS locators:
selenium.click("css=a.ButtonForward");
Using XPath locators:
selenium.click("xpath=/descendant::a[#class='ButtonForward']");