How can i get the value from div class in TestNG selenium? - 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);

Related

Find element in selenium using xpath

I'm trying to find and click the element "Test 123" using Selenium Webdriver in C#. I've tried all the methods I can think of, but no good. I think the values are hidden before they are selected, but not sure. Any ideas, please?
enter image description here
Simple Xpath locator can do the trick:
public void ClickElementByXpath(string text) {
IWebElement element = Driver.FindElement(By.XPath("//li[contains(text(), '${text}')]");
element.Click();
}
ClickElementByXpath("Test 123");

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();

Selenium and capture object

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();

How can i write my own xpath from the html code

I have followig HTML code and want X path for the text "Analytics & Research"
<div id="LLCompositePageContainer" class="column-wrapper">
<div id="compositePageTitleDiv">
<h1 class="page-header">Analytics & Research</h1>
</div>
I am getting following xpath using chrome, but that didnt work.
//*[#id="compositePageTitleDiv"]
this is my code
WebElement header = driver.findElement(By.xpath("//div[#id='LLCompositePageContainer']/div[#id='compositePageTitleDiv']/h1[#class='page-header']"));
String header2 = header.getText();
System.out.println(header2);
and following error I am getting
Exception in thread "main" org.openqa.selenium.NoSuchElementException:
Unable to find element with xpath ==
//div[#id='LLCompositePageContainer']/div[#id='compositePageTitleDiv']/h1[#class='page-header']
(WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 10.34 seconds For documentation on this
error, please visit:
http://seleniumhq.org/exceptions/no_such_element.html
Please try to use the below xpath:
driver.findElement(By.xpath(".//div[#id='compositePageTitleDiv']/h1")).getText();
If the element is inside the iframe. Then use the below code:
// Switching to the frame
driver.switchTo().frame(<name>);
// Storing the value of the Analytics & Research
String text = driver.findElement(By.xpath(".//div[#id='compositePageTitleDiv']/h1")).getText();
// Switching back to original window
driver.switchTo().defaultContent();
Hope this helps.
This is how it can be used :
WebElement element= driver.findElement(By.xpath("//*[#id='compositePageTitleDiv']"));
Or in case it is nested, can be accessed like this as well
WebElement element = driver.findElement(By.xpath("//html/body/div[3]/div[3]/"));
this is just a rough syntax.
No need to use Xpath here if you could simply locate the element using By.id(). Asuming are using Java, you should try as below :-
WebElement el = drive.findElement(By.id("compositePageTitleDiv"));
String text = el.getText();
Edited :- If element not found, may it is timing issues you need to implement WebDriverWait to wait for element until visible on the page as below :-
WebDriverWait wait = new WebDriverWait(webDriver, implicitWait);
WebElement el = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("compositePageTitleDiv")));
String text = el.getText();
Note :- if your element is inside any frame, you need to switch that frame before finding element as :- driver.switchTo().frame("your frame name or id");
Hope it helps..:)
You can also use
//div[#id='LLCompositePageContainer']/div[#id='compositePageTitleDiv']/
h1[contains(text(),'Analytics')]
This is the best way to reach to the specific web element, using contains minimize the chances of error.
The correct xpath is
//div[#id='LLCompositePageContainer']
/div[#id='compositePageTitleDiv']
/h1[#class='page-header']
But you could have find your answer easily with some researchs on google...

How to verify if the image in Selenium using Webdriver

How would I verify the image is displaying is the correct path/name in Selenium using WebDriver?
I started using this code but not sure :
string _active = "<img style="display: ;" alt="Active" src="../App_Themes/Default/images/check.png"/>";
driver.FindElement(By.XPath("//*[#id='ctl00_ContentPlaceHolder1_AddeCardControl1_gv']/tbody/tr[11]/td[7]/img")).Text.Contains(_active);
I would like to clarify to you that this code
driver.FindElement(By.XPath("//*[#id='ctl00_ContentPlaceHolder1_AddeCardControl1_gv']/tbody/tr[11]/td[7]/img")).Text.Contains(_active);
does not give you the html code for the image tag but a IWebElement object. And you can read the various attributes of this WebElement by using the GetAttribute method.
You will have to get the src attribute of the img tag you are looking for by locating the image(webelement) by xpath and then
IWebElement element = driver.FindElement(By.XPath("Your xpath"));
string path = element.GetAttribute("src");
Now you can verify the path of your image. Hope this helps you.