Selenium-Unable to identify the search box element using xpath - selenium

WebElement SearchField =
driver.findElement(By.xpath("/html/body/div[2]/div[3]/form/div[1]/div[1]/div[1]/div/div[2]/input"));
SearchField.sendKeys("my name");
Thread.sleep(5000);
WebElement SearchButton =
driver.findElement(By.xpath("/html/body/div[2]/div[3]/form/div[1]/div[1]/div[3]/center/input[1]"));
SearchButton.submit();
Thread.sleep(3000);
driver.quit();
In above code I have tried to select the search box in google.com and enter "my name" as the value.
but it says xpath element not found.
So I took the "name" instead of xpath element and it worked.
WebElement SearchField = driver.findElement(By.name("q"));
How can I run this using xpath?

Why you want to switch to xpath if name works fine ?
There are lot of advantage if you use ID, NAME, LINKTEXT, PARTIALLINKTEXT, CSSSELECTOR, CLASSNAME and lastly XPATH :
Anyway you can try this xpath :
//input[#name='q']
this is a relative xpath. The one you are using is absolute xpath. try to avoid that.

The unique XPath for the search input element you are looking is:
//input[#name='q' and #type='text']

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

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

Selenium C# how to get Element

Approve
How can I get this element in selenium C#. I tried by:href ,Xpath, ID, class name, text name but I couldn't get this element
you can click by Link text
driver.FindElement(By.LinkText("Approve")).Click();
or by CSS Selector
.btn.green.request-action-btn
To identify the element you can use the following Locator Strategy:
CssSelector:
driver.FindElement(By.CssSelector("a.btn.green.request-action-btn[data-request-status='Approved']"))
XPath:
driver.FindElement(By.XPath("//a[#class='btn green request-action-btn' and #data-request-status='Approved']"))
XPath using contains():
driver.FindElement(By.XPath("//a[#class='btn green request-action-btn'][contains(.,'Approve')]"))
XPath using normalize-space():
driver.FindElement(By.XPath("//a[#class='btn green request-action-btn'][normalize-space()='Approve']"))
It can be found by using tagName as below:
ReadOnlyCollection<IWebElement> anchrTags= driver.FindElements(By.TagName("a"));
IWebElement roWebElement = anchrTags.Where(x => x.Text == "Approve").FirstOrDefault();
Otherwise if the anchor tag exists in the frames, it will not found. try switching to that frame:
IWebElement iframe = driver.FindElements(By.Id("iframeId"));
driver.SwitchTo().Frame(iframe);
IWebElement roWebElement = anchrTags.Where(x => x.Text == "Approve").FirstOrDefault();

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