Selenium and capture object - selenium

Using firefox and marking a link in my web-app I get among other things, this code which I think I can use to caprture an object:
cb_or_somename_someothername cb_area_0219
This string is "classname" in Firebug.
Going to the script I type in:
WebElement rolle = driver.findElement(By.className("cb_or_somename_someothername cb_area_0219"));
But the script does not find the element when executing.
Other onfo in the Firebug panel is:
class="cb_or_somename_someothername cb_area_0219"
onclick="jsf.util.chain(this,event,'$(this).attr(\'disabled\', \'disabled\');return true;','mojarra.jsfcljs(document.getElementById(\'fwMainContentForm\'),{\'fwMainContentForm:j_idt156:2:selectRole \':\'fwMainContentForm:j_idt156:2:selectRole\'},\'\')');return false"
href="#"
id="fwMainContentForm:j_idt156:2:selectRole"
Is my script referring the element in a wrong way?

You cannot use search by compound class name (name with spaces). Try to use search by CSS selector instead:
WebElement rolle = driver.findElement(By.cssSelector(".cb_or_somename_someothername.cb_area_0219"));
or by XPath:
WebElement rolle = driver.findElement(By.xpath("//*[#class='cb_or_somename_someothername cb_area_0219']"));
Also you still can use search by one of two class names:
WebElement rolle = driver.findElement(By.className("cb_or_somename_someothername"));
or
WebElement rolle = driver.findElement(By.className("cb_area_0219")); // Note that this class name could be generated dynamically, so each time it could has different decimal part
Update
If you get Element is not clickable... exception it seem that your element is covered by another element at the moment you try to click on it. So try to wait until this "cover" is no more visible:
new WebDriverWait(driver, 10).until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//p[#class='environtment-banner']"));
WebElement rolle = driver.findElement(By.className("cb_area_0219"));
rolle.click();

Related

How can i get the value from div class in TestNG selenium?

I am using TestNG and selenium for testing web app.
<div class="infoMessage">The request has been successfully submitted.</div>
In the my TestNG class file, like any other HTML elments for div element also
I have
#FindBy(xpath="//*[#id='wrapper']/table/tbody/tr[1]/td/div")
WebElement resultdiv;
Now that I got the webelement in resultdiv, how can i read the content "The request has been successfully submitted" ?
Just at a quick glance, it seem like you can try use className instead xpath:
#FindBy(className="infoMessage")
WebElement resultdiv;
Use .getText(); to achieve:
String text = resultdiv.getText();
Hi #bnath002 You can use Contains text Xpath, below is the code. this code always work for text.
WebElement element = driver.findElement(By.xpath("//div[contains(text(),'The request has been successfully submitted.')]"));
String innerText= element.getText();
System.out.println("Your inner text is: "+innerText);
I'm a little unfamiliar with #FindBy, but i'm assuming you could use getText() as normal. But if everything went as planned and the xpath located your element successfully, you should be able to retrieve the text with the following :)
WebElement element = driver.findElement(By.xpath("//*[#id='wrapper']/table/tbody/tr[1]/td/div"));
String innerText= element.getText();
System.out.println("Your inner text is: "+innerText);

selenium element.click() not working (doesn't click)

String selector = ".rmcAlertDialog .buttons :first-child";
RemoteWebElement selection = (RemoteWebElement) driver.findElement(By.cssSelector(selector));
WebDriverWait wait = new WebDriverWait(driver, 60);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(selection));
if (element == selection) selection.click();
But the element in question (a button) is not responding to the click.
If I click the button manually it works so its not the web page at fault, but the automation.
I have verified the button is there by comparing it's text content.
updated for clarification
This code works (or worked) for most buttons. The code is from a script interpreter which is parsing:-
select ".rmcAlertDialog .buttons :first-child" click
This code was working prior to more recent versions of chrome/selenium/chromedriver.
The code now doesn't work for some buttons.
selection.click() IS being called (verified in a debugger), as element will always equal selection, it just is not working.
.buttons is the class name of the container div for the button(s)
The selector is not directing to the element with button class. You have a space between .button and :first-child in the selector. Remove the space. The given selector is searching for a child element of the tag with button class. But I'm assuming you are trying to click on the first element with button class not the child node of the button class element.
Use this:
String selector = ".rmcAlertDialog .buttons:first-child";
I think the main reason it's failing is because your if statement will never be true. I've never done any comparisons like this but you can simplify your code significantly and still get the desired effect.
A few suggestions:
Don't define locators as Strings, define them as Bys. The By class is defined for just such a task and makes using and passing them around MUCH easier.
String selector = ".rmcAlertDialog .buttons:first-child";
would turn into
By locator = By.cssSelector(".rmcAlertDialog .buttons:first-child");
Note the correction that S Ahmed pointed out in his answer.
You don't need to find the element to wait for it to be clickable. There is an overload that takes a By locator, use that instead.
RemoteWebElement selection = (RemoteWebElement) driver.findElement(By.cssSelector(selector));
WebDriverWait wait = new WebDriverWait(driver, 60);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(selection));
becomes
WebElement element = new WebDriverWait(driver, 60).until(ExpectedConditions.elementToBeClickable(locator));
Skip the RemoteWebElement and WebElement comparison. I don't think this will work and there's no need for it anyway. Your locator will locate the same element consistently.
So your final code should look something like
By locator = By.cssSelector(".rmcAlertDialog .buttons:first-child");
new WebDriverWait(driver, 60).until(ExpectedConditions.elementToBeClickable(locator)).click();

How to handle Auto suggestion in selenium?

/I want to select Mumbai as source and Delhi as destination from autosuggestion on cleartrip website. I have written below code. Here source is getting handled properly but on destination, autosuggestion list is displayed but nothing get selected from the list. Could someone please help me out/
String baseurl = "https://www.cleartrip.com/";
driver.get(baseurl);
String title = driver.getTitle();
System.out.println(title);
WebDriverWait wait=new WebDriverWait(driver, 20);
WebElement flighttab = driver.findElement(By.linkText("Flights"));
flighttab.click();
Thread.sleep(5000);
WebElement roundtrip_radio_button = driver.findElement(By.id("RoundTrip"));
roundtrip_radio_button.click();
WebElement from = driver.findElement(By.xpath(".//*[#id='FromTag']"));
WebElement to =driver.findElement(By.xpath(".//*[#id='ToTag']"));
from.clear();
from.sendKeys("Mumbai");
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//ul/li[#class='list']")));
driver.findElement(By.xpath("//ul/li[#class='list']")).click();
to.clear();
to.sendKeys("Delhi");
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//ul/li[#class='list']")));
driver.findElement(By.xpath("//ul/li[#class='list']")).click();
The xpath that you are using is incorrect. I am providing you the correct xpath and an alternate way to click on the auto-completer (by using className), you can use either of them, both will work fine. And as the wait.until method returns the element, you can directly perform the click on it, which would result in one atleast less operation/scraping on the page.
Autocompleter Correct Xpath:
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[#class='uiSelected']"))).click();
Autocompleter by className:
wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("uiSelected"))).click();

How to send values to textboxes that doesn't have input ids in selenium webdriver? I am using phptravles.net for testing?

phptravels location text field
Have attached a image with Html code.
I couldn't able to write a xpath to recognise and sendkeys.
The text field has different xpath when it is focused and when it is not.
Its throwing me NoSuchElementFound exception.
driver.get("http://www.phptravels.net");
WebElement e = driver.findElement(By.xpath("//*[#id='select2-search']/input"));
e.click();
e.sendKeys(city);
You can create xpath using other attributes of that element like
//input[contains(#attribute,'attribute_value')]
Eg. For a input element with class attribute "textbox", xpath can be written like
//input[contains(#class,'textBox')]
I tried with below two points and it worked for me,
1) modified xpath with span text()
2) Instead of e.sendKeys(), tried actions.sendKeys() as the former throwing 'cannot focus' error. Credit to link
driver.get("http://www.phptravels.net");
WebElement e = driver.findElement(By.xpath("//span[text()='Search by Hotel or City Name']"));
/* e.click();
e.sendKeys("GOA");
*/
Actions actions = new Actions(driver);
actions.moveToElement(e);
actions.click();
actions.sendKeys("GOA");
actions.build().perform();

Accessing a element in selenium

In Firebug I have a link with this content:
<a id="fwMainContentForm:j_idt156:2:selectRole" class="cb_or_somename cb_area_0219" onclick="jsf.util.chain(this,event,'$(this).attr(\'disabled\', \'disabled\');return true;','mojarra.jsfcljs(document.getElementById(\'fwMainContentForm\'),{\'fwMainContentForm:j_idt156:2:selectRole\':\'fwMainContentForm:j_idt156:2:selectRole\'},\'\')');return false" href="#">Somename</a>
How can I use a "classname" to access the link (clicking on it)?
I have tried the following:
WebElement rolle = driver.findElement(By.className("cb_or_somename cb_area_0219"));
But I get the error:
Exception in thread "main"
org.openqa.selenium.InvalidSelectorException: The given selector cb_or_somename cb_area_0219 is either invalid or does not result in a WebElement.
The following error occurred:
InvalidSelectorError: Compound class names not permitted
cb_or_somename cb_area_0219 is actually two classes. By.className can receive one of them
WebElement rolle = driver.findElement(By.className("cb_or_somename"));
// or
WebElement rolle = driver.findElement(By.className("cb_area_0219"));
If you want to locate the element with both of them use cssSelector
WebElement rolle = driver.findElement(By.cssSelector(".cb_or_somename.cb_area_0219"));
To find element using both classes you can use the xpath instead of className.
WebElement rolle = driver.findElement(By.xpath("//*[contains(#class,'cb_or_somename cb_area_0219')]"));