selenium for Google search - java

I am trying to build a simple function in selenium that given a text brings back all the result from Google. I'm using xpath to find all result elements. Although "//a[#class='l']" works as a legit xpath with firebug the resulting list is empty when I run the code.
public void testSearch() {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
WebElement query = driver.findElement(By.name("q"));
query.sendKeys("obama twitter");
query.submit();
long end = System.currentTimeMillis() + 5000;
while (System.currentTimeMillis() < end) {
WebElement resultsDiv = driver.findElement(By.className("gssb_e"));
if (resultsDiv.isDisplayed()) {
break;
}
}
List<WebElement> weblinks = driver.findElements(By.xpath("//a[#class='l']"));
for (WebElement suggestion : weblinks) {
System.out.println(suggestion.getText()+"\n");
System.out.println("==> "+suggestion.getAttribute("href")+"\n");
}
}

Related

How to wait till text value is fully loaded in selenium

I tried the following code in Selenium to get the row values.
public static void main(String[] args) throws InterruptedException {
WebDriverManager.chromedriver().setup();
ChromeDriver driver = new ChromeDriver();
driver.get("https://www2.asx.com.au/");
driver.manage().window().maximize();
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
try {
WebElement cookies = driver.findElementByXPath("//button[text()='Accept All Cookies']");
wait.until(ExpectedConditions.elementToBeClickable(cookies)).click();
} catch (Exception e) {
System.out.println("cookies pop up not found");
}
WebElement el = driver
.findElementByXPath("//*[#class='markit-home-top-five aem-GridColumn aem-GridColumn--default--12']");
wait.until(ExpectedConditions.visibilityOf(el));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", el);
List<WebElement> elements = driver.findElementsByXPath("//caption[text()='Gains']//ancestor::table//tr");
for (int i = 1; i <= elements.size(); i++) {
Thread.sleep(2000);
System.out.println(driver.findElementByXPath(
"//caption[text()='Gains']//ancestor::table//tr[" + i + "]//span[#class='value-with-arrow']")
.getText());
}
}
I faced two issues:
The 'Allow all cookies' button is not getting clicked and the pop up remains there. 'cookies pop up not found' is getting printed in the output.
The value of the tables are not getting printed without sleep. in the below output ,it is shown that the first text value is printed as -- as the value was still loading. How to provide till the value is fully loaded.
Thanks in advance!
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.textToBePresentInElementLocated(By.id("element_id"), "The Text"));
use webdriver wait , you can use expected condition texttobepresent or element to be present
https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html#textToBePresentInElementLocated(org.openqa.selenium.By,java.lang.String)

Cannot find element using Selenium Webdriver

I am trying to navigate to "https://developers.google.com/".
I then click a link by the xpath and redirect to another page.
Everything works as far as finding elements and clicking links on the first page.
But I cannot seem to find the elements I want to look for after I go to the new page.
The redirected page is "https://cloud.withgoogle.com/next18/sf/?utm_source=devsite&utm_medium=hpp&utm_campaign=cloudnext_april18"
This is checking if text is equal.
confirmText("Imagine", "//*[#id=\"main\"]/span/div[2]/div/div/div[1]/div[1]/div/div[1]/div[2]/h3");
public static void confirmText(String text, String xpath) {
System.out.println("Trying to confirm that given string is equal to the text on page.");
WebElement element = driver.findElement(By.xpath(xpath));
System.out.println("Test case: " + text);
System.out.println("Result: " + element.getText());
if (element.getText() == text) {
System.out.println("\nEquals.");
}
else {
System.out.println("\nDoes not equals.");
}
System.out.println("\n\n");
}
This is sending keys.
public static void sendKeys() {
WebElement firstname = driver.findElement(By.id("firstName"));
firstname.sendKeys("John");
WebElement lastname = driver.findElement(By.xpath("//*[#id=\"lastName\"]"));
lastname.sendKeys("Doe");
WebElement email = driver.findElement(By.xpath("//*[#id=\"email\"]"));
email.sendKeys("johndoe#gmail.com");
WebElement jobtitle = driver.findElement(By.xpath("//*[#id=\"jobTitle\"]"));
jobtitle.sendKeys("Software Engineer");
WebElement company = driver.findElement(By.xpath("//*[#id=\"company\"]"));
company.sendKeys("ABCD");
}
The error is
Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id="main"]/span/div[2]/div/div/div[1]/div[1]/div/div[1]/div[2]/h3"}
Only the first page actually waits to finish loading before the script starts. If you navigate away form the page then you will need to manually wait. Without waiting, the script will continue to the next command immediately after clicking the link that changes the page, and since the page is not loaded yet you will not find the element. This gives the error.
The simplest way to wait is by using "WebDriverWait"
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id(>someid>)));
There are more simple examples here:
http://toolsqa.com/selenium-webdriver/wait-commands/
You can try this xpath using for imagine and put some wait as #chris mentioned
//div//h3[contains(text(),'Imagine')]
JavascriptExecutor can be used to get the value of an element,
Refer code,
confirmText("Imagine", "//*[#id=\"main\"]/span/div[2]/div/div/div[1]/div[1]/div/div[1]/div[2]/h3");
public static void confirmText(String text, String xpath) {
System.out.println("Trying to confirm that given string is equal to the text on page.");
WebElement element = driver.findElement(By.xpath(xpath));
JavascriptExecutor executor = (JavascriptExecutor)driver;
System.out.println("Test case: " + text);
System.out.println("Result: " + executor.executeScript("return arguments[0].innerHTML;", element););
if (text.equals(executor.executeScript("return arguments[0].innerHTML;", element))) {
System.out.println("\nEquals.");
}
else {
System.out.println("\nDoes not equals.");
}
System.out.println("\n\n");}

How to locate Web elements (developed using div tags) using Selenium WebDriver if xpath fails

I am trying to get the count of all mails in Gmail and the ">" button to go to older emails can't be located using Selenium, WebDriver. As it was developed using div tags, I could only use Xpath but no luck. Any help please?
the snippet of my code is below:
int allmails = 0;
int mails = 0;
do
{
WebElement table = obj.findElement(By.xpath("/html/body/div[7]/div[3]/div/div[2]/div[1]/div[2]/div/div/div/div[2]/div[1]/div[1]/div/div/div[4]/div[1]/div/table/tbody"));
List<WebElement> rows = table.findElements(By.tagName("tr"));
mails = rows.size();
allmails = allmails + mails;
if (obj.findElement(By.xpath("/html/body/div[7]/div[3]/div/div[2]/div[1]/div[2]/div/div/div/div[1]/div[2]/div[1]/div[2]/div[1]/span/div[3]")).getAttribute("aria-disabled").equals(""))
{
System.out.println("Total Emails", +allmails);
break;
}
else
{
obj.findElement(By.xpath("/html/body/div[7]/div[3]/div/div[2]/div[1]/div[2]/div/div/div/div[1]/div[2]/div[1]/div[2]/div[1]/span/div[3]")).click();
Thread.sleep(2000);
}
}while(2>1);
obj.close();
}
}

Phantomjs' selenium Ajax code not loaded

I just discovered Selenium and I'm trying to learn how to use it with PhantomJS. The first example I've found was about getting a list of links from booking.com.
I tried running it with PhantomJS without luck. Firefox runs it well. The code in java looks like this:
private void start() {
Capabilities caps = new DesiredCapabilities();
((DesiredCapabilities) caps).setJavascriptEnabled(true);
((DesiredCapabilities) caps).setCapability("takesScreenshot", true);
((DesiredCapabilities) caps).setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "D:\\phantomjs-2.0.0-windows\\bin\\phantomjs.exe");
//WebDriver driver = new PhantomJSDriver(caps);
WebDriver driver = new FirefoxDriver();
driver.get("http://www.booking.com");
driver.findElement(By.id("destination")).sendKeys("Berlin");
//saveSShot(driver, "sel1.png");
long timeOut = 5000;
long end = System.currentTimeMillis() + timeOut;
while (System.currentTimeMillis() < end) {
if (String.valueOf(
((JavascriptExecutor) driver)
.executeScript("return document.readyState"))
.equals("complete")) {
break;
}
}
//saveSShot(driver, "sel2.png");
try {
//writeFile(driver, "output1.txt");
new WebDriverWait(driver, 3).until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("ul.ui-autocomplete li a"))).click();
//writeFile(driver, "output2a.txt");
//saveSShot(driver, "sel3.png");
driver.findElement(By.name("idf")).click();
driver.findElement(By.className("b-searchbox-button")).submit();
List<WebElement> list = driver.findElements(By
.cssSelector("a.hotel_name_link"));
for (WebElement webElement : list) {
System.out.println(webElement.getText());
}
} catch (TimeoutException e) {
System.out.println(e.toString());
//writeFile(driver, "output.txt");
}
}
Note the two declarations of driver. If I use Firefox, the WebDriverWait works. On PhantomJS it gives an error with WebDriverWait (Element not found :262 in error)
I have added all code. You can paste it in a new project, add the class and includes and you'll see how it (not) work. The two commented method saveSShot and writeFile must be written. I used them for debugging purposes. What I see on the second screenshot is that "Berlin" is in fact written, but the ajax dropdown isn't there. With Firefox it appears.
This has nothing to do with ghostDriver: this is a generic WebDriver usage case. You need to define such a scenario yourself, most probably by registering some JS in the page to do the check for you, and use the driver to pick up the result. You should provide a mechanism that waits for this element to be displayed and enabled. Waiting for the page's ready-state. Such simple solution can do the trick:
long timeOut = 5000;
long end = System.currentTimeMillis() + timeOut;
while (System.currentTimeMillis() < end) {
if (String.valueOf(
((JavascriptExecutor) driver)
.executeScript("return document.readyState"))
.equals("complete")) {
break;
}
}
I've used the ghostDriver for rich content (gaming sites) and it worked pretty well with all the AJAX.
I did some research and this is what I find working too:
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));
wait.Until(d => (bool)(d as IJavaScriptExecutor).ExecuteScript("return jQuery.active == 0"));
Here is an article that can help you if you decide to proceed with the JS code.

Selenium attempts connection to localhost while browsing to external URL

I am using Selenium v2.30.0 and Firefox 19.0. When I execute the below code:
public class First_Example {
public static void main(String[] args) {
// Create a new instance of the Firefox driver
// Notice that the remainder of the code relies on the interface,
// not the implementation.
WebDriver driver = new FirefoxDriver();
// And now use this to visit Google
driver.get("http://www.google.com");
// Alternatively the same thing can be done like this
// driver.navigate().to("http://www.google.com");
// Find the text input element by its name
WebElement element = driver.findElement(By.name("q"));
// Enter something to search for
element.sendKeys("Cheese!");
// Now submit the form. WebDriver will find the form for us from the element
element.submit();
// Check the title of the page
System.out.println("Page title is: " + driver.getTitle());
// Google's search is rendered dynamically with JavaScript.
// Wait for the page to load, timeout after 10 seconds
(new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.getTitle().toLowerCase().startsWith("cheese!");
}
});
// Should see: "cheese! - Google Search"
System.out.println("Page title is: " + driver.getTitle());
//Close the browser
driver.quit();
}
}
I get an error saying:
"Unable to connect to host localhost on port 7055 after 45000 ms"
I've searched everywhere for an answer with no success. All help appreciated.
Seems like some problem while waiting for the response
try this code instead:
public static void main(String[] args)
{
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com/");
WebElement query = driver.findElement(By.name("q"));
query.sendKeys("Cheers!");
// Sleep until the div we want is visible or 5 seconds is over
long end = System.currentTimeMillis() + 5000;
while (System.currentTimeMillis() < end) {
WebElement resultsDiv = driver.findElement(By.className("gssb_e"));
// If results have been returned, the results are displayed in a drop down.
if (resultsDiv.isDisplayed()) {
break;
}
}
// And now list the suggestions
List<WebElement> allSuggestions = driver.findElements(By.xpath("//td[#class='gssb_a gbqfsf']"));
for (WebElement suggestion : allSuggestions) {
System.out.println(suggestion.getText());
}
}
You need to update Selenium to v2.31. Firefox 19 is not supported in anything lower.
https://code.google.com/p/selenium/downloads/list

Categories