I am setting up a test to perform a search and after the search is complete, i want to capture the results line that says "About xxx results (x.xx seconds)"
Here's the code snippet
FirefoxDriver driver = new FirefoxDriver();
driver.get("http://google.com");
driver.manage().window().maximize();
WebElement searchBox = driver.findElementById("lst-ib");
searchBox.sendKeys("search text");
WebElement clickSearch = driver.findElementByXPath("html/body/div/div[3]/form/div[2]/div[2]/div[1]/div[1]/div[2]/div/div/div/button");
clickSearch.click();
WebElement results = driver.findElementByXPath("html/body/div[1]/div[5]/div[4]/div[5]/div[1]/div[1]/div/div/div");
System.out.println(results);
You need to use getText() method to get the text form a element. In your case you need to do something like below,
WebElement results = driver.findElement(By.xpath("html/body/div[1]/div[5]/div[4]/div[5]/div[1]/div[1]/div/div/div"));
System.out.println(results.getText());
The line you need to capture would have the selector:
Css:
#resultStats
Xpath:
//div[#id='resultStats']
If you need to return the text:
//div[#id='resultStats']//text()
Or use find and getText() method.
Related
I looked through a lot of topics on Stackoverflow and tried several recommendations but could hardly succeed in resolving my particular case. I'm trying to automate Google Cloud Pricing Calculator using Selenium WebDriver + Java. I need insert the Number of instances (the first input area on the page) using Java code.
My Java code is following:
WebDriver driver = new ChromeDriver();
driver.get("https://cloud.google.com/products/calculator");
new WebDriverWait(driver, 10)
.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//iframe[contains (#name, 'goog_')]")));
WebElement document = driver.findElement(By.xpath("//iframe[contains (#name, 'goog_')]"));
var iframe = document.findElement(By.xpath("//iframe[#id='myFrame']"));
var input = iframe.findElement(By.xpath("//input[#id='input_66']"));
input.click();
input.sendKeys("4");
But NoSuchElementException is thrown when I launch the code: "no such element: Unable to locate element: {"method":"xpath","selector":"//iframe[#id='myFrame']"}.
Xpath is correct but this element is hidden deep into html tree. How can I reach for the element (Number of instances) in this particular case? Thank you in advance!
You first need to switch to the parent iframe element, then switch into the inner iframe and only after that try accessing the input element.
I see you tried to locate the iframe elements but you do not switch into them.
Try this:
WebDriver driver = new ChromeDriver();
driver.get("https://cloud.google.com/products/calculator");
//Find and switch to outer iframe
new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//iframe[contains(#src,'product')]")));
driver.switchTo().frame(driver.findElement(By.xpath("//iframe[contains(#src,'product')]")));
driver.switchTo().frame(driver.findElement(By.xpath("//iframe[#id='myFrame']")));
WebElement input = driver.findElement(By.xpath("//input[#id='input_66']"));
input.click();
input.sendKeys("4");
Can you try with the below line of code?
WebDriver driver = new ChromeDriver();
driver.get("https://cloud.google.com/products/calculator");
new WebDriverWait(driver, 10)
.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//iframe[contains (#name, 'goog_')]")));
WebElement document = driver.findElement(By.xpath("//iframe[contains (#name, 'goog_')]"));
driver.switchTo().frame(document).switchTo().frame("myFrame");
//var iframe = document.findElement(By.xpath("//iframe[#id='myFrame']"));
WebElement ele = driver.findElement(By.xpath("//input[#id='input_66']"));
ele.click();
ele.sendKeys("4");
I have problem to automate this drop down using selenium web driver using Java
This is the link - Go to 5th drop down named as Github users (fetch. js)
I am not able to enter the data into search field.
I am using send keys after perform click but it throws an exception like this " element is not interact able"
Steps I follow
driver.findElement(By.xpath("xapth")).click
drop down opens with no options because it is searchable and options are coming dynamically after entering key word into the search field.
driver.findElement(By.xpath("xapth")).sendkeys("Test");
Sendkeys are not working in this case because of drop down closed when perform send keys action.
<div class="Select-placeholder">Select...</div>
Below is the code which is working.
Please do optimize the code by removing thread.Sleep and putting some meaningful waits as per your requirement.
driver.Navigate().GoToUrl("https://jedwatson.github.io/react-select/");
IWebElement element1 = driver.FindElement(By.XPath("//span[#id='react-select-6--value']"));
IWebElement element2 = driver.FindElement(By.XPath("//span[#id='react-select-6--value']/div[2]/input[1]")) ;
element1.Click();
Thread.Sleep(2000);
element2.SendKeys("Test");
Thread.Sleep(1000);
element2.SendKeys(Keys.Tab);
Please note that element2 gets activated once you click on element1.
Try the following code:
public void typeAndSelect() {
WebElement searchBox = driver.findElement(By.xpath("//div[#class='section'][5]//div[#class='Select-control']"));
searchBox.click();
WebElement inputField = driver.findElement(By.xpath("//div[#class='section'][5]//input[#role='combobox']"));
inputField.clear();
String searchWord = "test";
inputField.sendKeys(searchWord);
WebElement selectDropdown = driver.findElement(By.xpath("//div[#class='Select-menu-outer']//div[#role='option'][text()='" + searchWord +"']"));
// wait for search results.
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(selectDropdown)).click();
}
Correct the following xpath part
"//div[#class='section'][5]"
to your implementation of the dropdown
I have been unable to sendKeys text into the username and password field on ebay.
Here is the code:
WebDriver driver = null;
System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver");
driver = new ChromeDriver();
driver.get("http://www.ebay.co.uk");
WebElement myEbay = driver.findElement(By.linkText("My eBay"));
myEbay.click();
WebElement signInForm = driver.findElement(By.id("SignInForm"));
if (signInForm.isDisplayed())
System.out.println("Sign in form is displayed");
WebElement username;
username = driver.findElement(By.cssSelector("input[placeholder=\"Email or username\"]"));
It manages to find the My Ebay link, and verifies that the sign in form exists but the the username and password fields id's change after every refresh of the page.
The username cssSelector seems to be the problem??
EDIT: I have been successful using XPath but this excercise was to make the cssSelector work as there is no reason in theory why it shouldn't!
I have replaced the cssSelector locator with xpath and it is working fine with absolute xpath. Here is the modified code:
WebDriver driver = null;
System.setProperty("webdriver.chrome.driver", "C:\\Users\\vikas\\workspaceNeon\\Eclipse Soft\\chromedriver.exe");
driver = new ChromeDriver();
driver.get("http://www.ebay.co.uk");
WebElement myEbay = driver.findElement(By.linkText("My eBay"));
myEbay.click();
WebElement signInForm = driver.findElement(By.id("SignInForm"));
if (signInForm.isDisplayed())
System.out.println("Sign in form is displayed");
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
WebElement username = driver.findElement(By.xpath("html/body/div[4]/div/div/div/div[5]/div/div[1]/div/div[1]/div[1]/div[2]/div/span/form/div[1]/div[5]/div/div[4]/span[2]/input"));
username.sendKeys("Vikas");
Select all inputs as a list and search for the right one:
// fld seems to be the class of the input field but looking at all input elements should work too
List<WebElement> inputs = driver.findElements(By.className("fld"));
for (WebElement input: inputs) {
if (inputs.getAttribute("placeholder") == "Email or username") {
// ...
}
}
if the username and password webelements changes frequently find div section in the page using xpath and get the exact xpath of username weblement.
Hope this help..
I am getting this error while trying to write to simple code in selenium webdriver to enter a value in google search page and enter.
Following is my code -:
WebDriver driver = new FirefoxDriver(profile);
driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
driver.get("http://www.google.com");
WebElement element=driver.findElement(By.xpath("//input[#id='gs_htif0']"));
boolean b = element.isEnabled();
if (b){
System.out.println("Enabled");
}
element.sendKeys("Test Automation");
element.submit();
Can anyone please help me out with this? How to enable a disabled element?
You are using the wrong 'input' for entering the text. You should be using the following XPath:
//input[#name='q']
Like
WebElement element=driver.findElement(By.xpath("//input[#name='q']"));
This 'input' element accepts the input text just fine.
You can try to run javascript on page:
((JavascriptExecutor) driver).executeScript("document.getElementById('gs_htif0').disabled = false");
or
((JavascriptExecutor) driver).executeScript("arguments[0].disabled = false", element);
See, if this might help,
WebDriver driver = new FirefoxDriver(profile);
driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
driver.get("http://www.google.com");
WebElement element = driver.findElement(By.name("q"));
if(element.isEnabled()) {
System.out.println("Enabled");
element.sendKeys("Test Automation");
element.submit();
}
Try this:
WebDriverWait wait = new WebDriverWait(driver, 40);
driver.get("http://www.google.com");
wait.until(ExpectedConditions.visibilityOfElementLocated(By
.xpath("//input[#id='gs_htif0']")));
driver.findElement(By.xpath("//input[#id='gs_htif0']"))
.sendKeys("Test Automation" + Keys.ENTER);
Or:
public boolean isElementPresent(WebDriver driver, By by)
{
try {
driver.findElement(by);
System.out.print("Enabled");
return true;
} catch (NoSuchElementException ignored) {
return false;
}
}
isElementPresent = isElementPresent(
driver, By.xpath("//input[#id='gs_htif0']"));
if (isElementPresent) {
element.sendKeys("Test Automation");
element.submit();
}
Or change xPath to name selector.
If i am correct then you are using the firebug add-on in the firefox driver to get the path for the searchbox. But the firebug seems to provide a path where the Id for the searchbox is not correct. If you use the inspect element option you can see the difference (in the below image you can spot the difference yourself).
Page has image with hyperlink and that hyperlink has target="_blank" and every time i press that image loads new firefox and that hyperlink is redirected to that new firefox web
and i lose all control of that webpage.
Is possilble to remove or change that target="_blank" on hyperlink, bcause i want to load webpage in same webdriver
WebDriver driver = new FirefoxDriver();
driver.get("http://www.page.eu/");
WebElement submit;
submit = driver.findElement(By.xpath("//img[#alt='page']"));
submit.click();
that hyperlink have target="_blank"
i need to change that target somehow by using webdriver + javascript maybe or what?
is it possible?
edited
thanks for suggestions, but still is this problem
i tried to make like Grooveek said but no changes
WebElement labels2 = driver.findElement(By.xpath("//a[#href='http://tahtpage.net']"));
WebElement aa = (WebElement) ((JavascriptExecutor) driver).executeScript("labels2.setAttribute('target','_self')",labels2 );
aa.click();
i have an error
org.openqa.selenium.WebDriverException: null (WARNING: The server did not provide any stacktrace information)
i'm not good at javascrit so i think is problem in that executor
Instead of clicking on the image, you could just directly go to the URL in the link:
WebElement link = (driver.findElement(By.xpath("//img[#alt='page']/parent::*"));
String href = link.getAttribute("href");
driver.get(href);
Try the following:
WebElement labels2 = driver.findElement(By.xpath("//a[#href='http://tahtpage.net']"));
WebElement aa = (WebElement) ((JavascriptExecutor) driver).executeScript("arguments[0].setAttribute('target','_self')",labels2 );
aa.click();
You are getting a null exception because you are using labels2 in your javascript, which doesn't exist in that context. By changing it to arguments[0] Selenium will take the labels2 parameter and reference it in javascript appropriately.
Evaluating javascript in the window will help you to suppress target=blank links
Here's the example from the Webdriver docs
List<WebElement> labels = driver.findElements(By.tagName("label"));
List<WebElement> inputs = (List<WebElement>) ((JavascriptExecutor)driver).executeScript(
"var labels = arguments[0], inputs = []; for (var i=0; i < labels.length; i++){" +
"inputs.push(document.getElementById(labels[i].getAttribute('for'))); } return inputs;", labels);
Adapt it to modify the DOM to throw target="_blank links"
Why don't you wanna use SwitchTo().Window?
I think you should use the SwitchTo().Window as suggested by simeon sinichkin. however, i didn't like his example.Here is simple example.
driver.Navigate().GoToUrl(baseURL);
//Get the Main Window Handle
var baseWindow = driver.CurrentWindowHandle;
// navigate to another window (_blank)
driver.FindElement(By.Id("btn_help")).Click();
Thread.Sleep(2000);
//switch back to Main Window
driver.SwitchTo().Window(baseWindow);
hope that helps
Why don't you use:
target="_self"