Clicking on Google Result Suggestions - java

Just for learning purpose, I am trying to click on the third element of the Google Results Suggestions
In the above picture, i want to click on qubool hai. My code gets the result suggestions and clicks the 3rd element.
List<WebElement> resultsuggestion = driver.findElements(By.cssSelector(".gssb_m > tbody:nth-child(1) > tr"));
new Actions(driver).click(resultsuggestion.get(2));
But Selenium doesn't click on it. Kindly let me know if anything wrong in the above code or suggest me alternative solutions

Try changing your code to:
WebElement result = driver.findElement(By.cssSelector(".gssb_m > tbody > tr:nth-child(3)"));
result.click();
using the :nth-child typically is necessary for specifically identifying children. You seem to be trying to find multiples of only 1 tbody.
furthermore, using the Actions class for a simple click is very unnecessary when you have the WebElement#click method.

Following is another way I found:
WebElement result = driver.findElement(By.cssSelector(".gssb_m tr:nth-of-type(3)"));
result.click();
.gssb_m tr:nth-of-type(3) : Under class='gssb_m' element, it looks for third tr tag.

Related

Is there a better way to programatically select a value from select2 dropdown using selenium WebDriver?

I am using Selenium WebDriver to automate something. It requires filling a form that involves selecting a value from a select2 dropdown. This is the code snippet that I am using-
final By SELECT_DIV = By.id("s2");
click(SELECT_DIV);
final By INPUT = By.cssSelector(".select2-drop-active .select2-input");
waitForVisibilityOfElement(INPUT);
enterCharSequence(INPUT, "someData");
waitForJSandJQueryToLoad(30);//30 seconds
final By LIST_ITEM = By.cssSelector(".select2-drop-active ul.select2-results li.select2-result-selectable");
click(LIST_ITEM);
FYI, there are no unique ids assigned to some of these elements and hence I used css selectors for locating them.
This code works but it sometimes throws a StaleElementReferenceException. This is the error:
org.openqa.selenium.StaleElementReferenceException: Element not found in the cache - perhaps the page has changed since it was looked up
For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html
Selenium version : 2.53
So, I want to know if there is any way I could avoid this. I read a few posts about it but they were not of much help.
Let me know if you need more information. Any help would be appreciated.
'StaleElementReferenceException' means that the element has changed. It is in another div, another span or the its properties changed. Selenium may found it but it changed the very second you tried to click on it.
You need to search for the same element again and wait for it to be clickAble or visible. For example:
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement button =
wait.until(ExpectedConditions.
elementToBeClickable(By.id("btnLogin")));

How can I scroll inside of an element in selenium using java?

I am trying to scroll inside of an element using selenium but not able to succeed so far.
I tried some solutions I found on the web but without success.
I would like to select the last user from the following list.
Is anyone knows how can I do that? Notice that all the elements in the list have the same locator.
Thanks
I hope this would do the trick:
Select select = new Select( driver.findElement(by) );
select.selectByIndex( select.getOptions().size() - 1 );
Please try with below code. If possible please share the test URL then I will replicate it from my side.
//get all the options from the dropdown list-
List<WebElement> allOptions = driver.findElements(By.xpath(""));
Actions action = new Actions(driver);
//select last user from the list
action.doubleClick(allOptions.get(allOptions.size()-1)).perform();

Dynamic Element id in selenium #FindBy

Can someone throw some light on how to dynamically retrieve List of Id by PageSection.
Our Page Contains number of sections based on number of person information.each person is a Form based PageSection.
Here is an example.
#FindBy(css = "#personForm0 > fieldset > div.apisInfoRequired.statusBox.active > div.btn.action.addBtn > input")
public PageElement addInfantBtn;
#FindBy(css = "#personForm1 > fieldset > div.apisInfoRequired.statusBox.active > div.btn.action.addBtn > input")
public PageElement addAdultBtn;
#personForm0 can be go upto #personForm9, now when i read that using #FindBy, I am facing difficulty with selenium. Can someone share your thought on how to write this.
Are you able to find all elements that has a specific name or similar and the iterate over them and determine if you have found what are are searching for?
That is, instead of using
findElement(By.name("q"));
are you able to use
findElements(By.name("q"));
and iterate on the list of web elements that are returned?
An example of the html you are working with would probably give you a better answer.

WebDriver can't get dropdown menu element (Java)

I write a script on Java for Selenium WebDriver, and I have a problem with selected from dropdown menu.
Here's my locator:
new Select(driver.findElement(By.id("FormElement_select_68_input_input"))).selectByVisibleText("Image");
Here's an error: http://prntscr.com/7jul03
Here's HTML code: http://prntscr.com/7jvou6
Need to select "Image" from this menu, but have an error.
Before I had the error like this, I can't upload file, it was because I need to switch to frame(0).
But here I don't know why I can't select menu "Image" from DropBox.
Your ID is dynamic, so you can't use it. Select will not work in your case, you just need to use two clicks
WebElement dropdown = driver.findElement(By.xpath("//div[#class='select-pad-wrapper AttributePlugin']/input"));
dropdown.click();
WebElement element = driver.findElement(By.xpath("//div[#class='select-pad-wrapper AttributePlugin']/div/ul/li[text()='Image']"));
element.click();
It looks like the element id you're looking for"FormElement_select_68_input_input" doesn't exist in your html, your code sample shows "FormElement_select_283_input_container" as the select box element. Try this:
Select droplist = new Select(driver.findElement(By.Id("FormElement_select_283_input_container")));
droplist.selectByVisibleText("image");
Because it is not Select tag.
Try with below logic
WebElement div = driver.findElement(By.cssSelector("div[id*='FormElement_'] > div > div"));
div.click();
WebElement li = div.findElement(By.xpath(".//ul/li[text()='Image']"));
li.click();
As per HTML code screen, i am expecting Select class (selectByVisibleText etc) does not work. can you do one thing, try to click on required option directly. (may be click on "//div[#class='selectbox-wrapper']/ul/li[#class='selectbox_li'][contains(text(),'Image')]" , check one is it correct or not in firepath)
Let me know the result.. if it does not work, as said above you need to click on that input dropdown box and need to click on that Image.
Thank You,
Murali

Selenium webdriver: How can I click these links?

<a onclick="requestReportGeneration('857f23e1baa767622a91f970963d8918', 'reportDiv31','CSV')" href="javascript:void[0];">CSV</a>
<a onclick="requestReportGeneration('64107e36323e5877c986edc98a17b6e8', 'reportDiv32','CSV')" href="javascript:void[0];">CSV</a>
<a onclick="requestReportGeneration('2cad4d4e5c8855c47a88b6ddf8345735', 'reportDiv33','CSV')" href="javascript:void[0];">CSV</a>
I have these three links on a page and I want to click each one in turn. I am reading all the links on the page into a list of WebElements and then I go through each one in turn if the href contains javascript:void[0] I then try to click it:
for (int i = 0; i < allLinks.size(); i++) {
String reportLink = allLinks.get(i).getAttribute("href");
if (reportLink.contains("javascript:void[0];"))
{
allLinks.get(i).click();
/// Do some more stuff
}
The problem is I keep getting an error saying the element is not visible. I have also tried just loading the page and instead of getting all the links doing
driver.findElement(By.xpath("//a[contains(#href,\"javascript:void[0]\")]")).click();
but that also just gives element not visible error.
Can anybody tell me why this isn't working?
Try putting a breakpoint before the while loop, debug your script, then step into it and use Eclipse's Display tab to interrogate what is up with the links. Try evaluating sample statements like:
allLinks.size();
allLinks.get(i).isDisplayed();
allLinks.get(i).isEnabled();
There must be something odd about the links (or the way webdriver is seeing them), but debugging like this will let you find out what it is.

Categories