Find element in selenium using xpath - selenium

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

Related

Selenium-Unable to identify the search box element using xpath

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']

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

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 do I check if an element is present on a webpage?

I'm using BrowserFactory in selenium and not WebDriver. I would like to check if an element is present on the DOM. If yes click it, otherwise do something else. There should be a simple solution to it but I'm not using WebDriver. If I do use it, it opens a new browser window and does everything in that.
Can you try this method to verify if the element is present
public static void isElementPresent(WebElement element) {
//log.info("Checking if element is present");
if(element.isDisplayed();){
element.click();
}
}

How to find element in frame in Selenium

I used below code for finding the element in the frame but I got the error
kindly explain by this framebyinedex, framebystring, framebywebelement:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Framebyindex
{
public static void main(String[] args)
{
WebDriver f1=new FirefoxDriver();
f1.get("http://spicareers.com/spicareers/");
f1.switchTo().frame(1);
f1.findElement(By.linkText(" .Net - Senior Developer ")).click();
}
}
The error is:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"link text","selector":" .Net - Senior Developer "}
Before you have to switch to the frame:
driver.get("http://spicareers.com/spicareers/");
WebElement frame= driver.findElement(By.xpath("//frame[#name='JRAMPSMainFrame']"));
driver.switchTo().frame(frame);
and after you could try with:
WebElement myEl= driver.findElement(By.xpath("//*[contains(text(), ' .Net - Senior Developer ')]"));
myEl.click();
Finally, if you want to switch back:
driver.switchTo().defaultContent();
EDIT
The interested element is inside the iframe with name "JRAMPSMainFrame". If you inspect your html, you could notice this:
NoSuchElementException simply means that, the selenium did not detect a link with the text as you asked it.
I suggest you to work with use by.xpath to resolve it. To get the xpath of the element, take the below steps on your chrome, or firefox browser.
Right click on the element (here the link)
Click on inspect. It opens the inspect console (Elements tab) in your browser, and highlights the clicked element.
Right click on the element in the elements tab
Choose copy
Copy Xpath
In your code, replace By.linkText with By.xpath
Paste the value as an string into the xpath("you paste here")
Or simpler, watch this video
Seems like you have selected a wrong frame or the link text might contain more spaces.
Chrome developer tools is your best friend in this situation. Use it to find the element correctly and to identify the frame.
SELECT FRAME
Open the application in chrome.
Open developer tools
Click Console tab of developer tool
Select you frame from the drop down
In the above picture there is only one frame.
FIND ELEMENT
After selecting the frame, type $x("//a[normalize-space(.) = '.Net - Senior Developer']") to check your xpath.
$x("") is the function for checking xpath in browser.
Using the above mechanism find the frame and the element.
Cheers!
If you take a closer look at the HTML of http://spicareers.com/spicareers/ the WebElement with text as .Net - Senior Developer is within an <iframe> tag. So before accessing the intended WebElement we have to switch to the frame first either through the frame_name, frame_id or frame_index as follows:
System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://spicareers.com/spicareers/");
driver.switchTo().frame("JRAMPSMainFrame"); //using "frame_name"
WebElement elem = driver.findElement(By.xpath("//*[contains(text(), ' .Net - Senior Developer ')]"));
elem.click();
You can use this code, First enter into the frame using name locator and try to locate the career option then
Try this code, I have modified your code:
public class Framebyindex
{
public static void main(String[] args)
{
WebDriver f1=new FirefoxDriver();
f1.get("http://spicareers.com/spicareers/");
f1.switchTo().frame( f1.findElement(By.name("JRAMPSMainFrame")));
WebElement netCareer= f1.findElement(By.xpath("//*[contains(text(), ' .Net - Senior Developer ')]"));
netCareer.click();
}
}
By using:
https://github.com/nick318/FindElementInFrames
you can write following:
SearchByFramesFactory searchFactory = new SearchByFramesFactory(driver);
SearchByFrames searchInFrame = searchFactory.search(() -> driver.findElement(By.xpath("//*[contains(text(), ' .Net - Senior Developer ')]")));
searchInFrame.getElem().ifPresent(WebElement::click);