My selenium webdriver script doesn't work on Chrome - selenium

package javapackage;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
public class SeleniumQuora {
public static void LaunchQuora()
{
System.setProperty("webdriver.chrome.driver","E:\\SBI SO\\Selenium\\Extracts\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("https://www.quora.com/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(13, TimeUnit.SECONDS);
driver.findElement(By.xpath("//*[#id='__w2_lIh8Ilg_google_connect_button']/span")).click();
}
public static void main(String[] args) {
LaunchQuora();}}
this code is supposed to click on "Continue with Google" option in the signIn page. But nothing happens. Its pretty basic I know but I searched most places and cant find the answer.

Actually you are locating wrong element, In this website there is no element with the id __w2_lIh8Ilg_google_connect_button as I seeing, May be provided id is dynamically generated, So if you want to click on Continue with Google button simply try using By.linkText() as below :-
System.setProperty("webdriver.chrome.driver","E:\\SBI SO\\Selenium\\Extracts\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-popup-blocking");
WebDriver driver = new ChromeDriver(options);
driver.get("https://www.quora.com/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(13, TimeUnit.SECONDS);
driver.findElement(By.linkText("Continue with Google")).click();
Hope it helps..:)

Related

ElementNotInteractableException: element not interactable in Selenium Java

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class E2E {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "D:\\Selenium\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.flygofirst.com/");
driver.findElement(By.xpath("//span[#id='onewaymodal-id']")).click();
Thread.sleep(25000);
driver.findElement(By.xpath("//div[#id = 'oneWaybd']//div[#class ='fromTo']/div[1]")).sendKeys("Ch");
}
}
I Tried to SendKeys in the From Text Box i was getting Element not Intractable.
And I provided Waiting time so that all the web elements will load. After that also i got the same exception Element not Intractable.
Can anyone help me with this
You have to modify the locator, try this one, its working:
// to handle the Accept Cookies button
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(15));
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#cookie-btn"))).click();
driver.findElement(By.xpath("//span[#id='onewaymodal-id']")).click();
Thread.sleep(1000);
// modified the below locator
driver.findElement(By.xpath("(.//div[#class='fromTo']//input[#id='roundTripbdFromView'])[2]")).sendKeys("ch");
I answered your previous question also, check that, if it works, mark that as the answer.

Not able to open new tab in Browser using Selenium Webdriver

I am trying to open new tab in selenium using below line of code
driver.findElement(By.cssSelector("body")).sendKeys(Keys.chord(Keys.CONTROL, "t"));
But tabs is not opening up. Can anyone tell whats wrong in this command?
Also can any one explain "driver.findElement(By.cssSelector("body"))" used in this command for ? I tried searching but not proper answers
Below complete is not working. It is opening up both gmail and stack overflow in same tab in chrome not opening up new tab
package TestCode;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Chrome {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver","C:\\Akash\\Drivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.gmail.com");
String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL+"t");
driver.findElement(By.tagName("body")).sendKeys(selectLinkOpeninNewTab);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://stackoverflow.com/");
System.out.println("Site open");
}
}
You can use javaScripts to open new tab in chrome.
try below line of code
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.open('https://www.google.com','_blank');");
Refer this link :- link
Instead of chord use control
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
For chrome use this
first opened the tab and then hit the URL
String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL+"t");
driver.findElement(By.tagName("body")).sendKeys(selectLinkOpeninNewTab);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://stackoverflow.com/");
System.out.println("Site open");

NoSuchElementException while executing my testscript

Observing following error while executing my test script. Can someone help me in identifying the cause of failure.
org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"id","selector":"lid"}
Command duration or timeout: 20.45 seconds
Here is the snippet of my code. Please note that the element exist in same frame hence frame switch is not needed.
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
//import org.openqa.selenium.support.ui.ExpectedConditions;
//import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;
public class LoginPage
{
#Test
public void testLoginFail()
{
WebDriver driver = new FirefoxDriver();
driver.get("https://www.zoho.com/crm/");
driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
driver.findElement(By.linkText("LOGIN")).click();
//WebDriverWait wait = new WebDriverWait(driver,40);
//wait.until(ExpectedConditions.visibilityOfElementLocated((By.id("lid"))));
driver.findElement(By.id("lid")).sendKeys("xyz#gmail.com");
HTML view of the element is:
<input name="lid" id="lid" class="input usrbx" value="" onkeypress="clearmsg()" type="email">
I have inspected the webpage "https://www.zoho.com/crm/lp/login.html" and i am able to see an iframe in it and the input text boxes are inside it. Below is the working code.
public static void main(String[] args)
{
System.setProperty("webdriver.chrome.driver", "C:\\Softwares\\Selenium\\Web Drivers\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("https://www.zoho.com/crm/");
driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
driver.findElement(By.linkText("LOGIN")).click();
//Switch to the frame
driver.switchTo().frame(0);
driver.findElement(By.id("lid")).sendKeys("xyz#gmail.com");
driver.quit();
}
Hope this helps you. Thanks.
Here is the Answer to your Question:
A few words about the Solution:
While working with Selenium 3.4.0 with geckodriver v0.16.1 & Mozilla Firefox 53.0 you need to download the latest geckodriver from here and provide the absolute path of the geckodriver through System.setProperty.
Try to avoid implicitlyWait as per recent updates implicitlyWait may die in fire soon.
The error you are seeing NoSuchElementException says it all. The id of the element is not traceable.
The element with id lid is within an iframe, so you need to switch to frame first.
Here is your own code with some simple tweaks in it:
#Test
public void testLoginFail()
{
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.zoho.com/crm/");
driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
driver.findElement(By.linkText("LOGIN")).click();
driver.switchTo().frame("zohoiam");
driver.findElement(By.xpath("//input[#id='lid']")).sendKeys("xyz#gmail.com");
}
Let me know if this Answers your Question.

Not able to click the Inline element using selenium webdriver

enter image description hereNot able to click the inline element using selenium webdriver.
Here is the URL
https://www.google.com/.
Besides Images link (Right side top) there is a square icon. Need to click that icon and select Maps.
Screenshot attached.
I used xpath, cssselector, ID, Name but nothing is working.
Could anyone help me on this.
Code:
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class webelements2 {
public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.gecko.driver","C:\\Users\\rpremala003\\Downloads\\geckodriver-v0.14.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com/");
driver.manage().window().maximize();
driver.findElement(By.id("gbwa")).click();
driver.findElement(By.className("gb_3")).click();
}
}
This code is somehow not working in Firefox because it opens the Products page when you click on element - By.id("gbwa"). But if you try the same in Chrome, it works fine. Only thing you would need to change is By.className("gb_3") with By.xpath("//ul[#class='gb_ka gb_da']/li[3]").
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com/");
driver.manage().window().maximize();
driver.findElement(By.id("gbwa")).click();
driver.findElement(By.xpath("//ul[#class='gb_ka gb_da']/li[3]")).click();
For Firefox, you can modify the code so that when the products page is opened, you can click on the Maps option from there.
Some dynamic waits need to be added for controls. Additionally, I noticed that, if google is not set as your home page then you see a message 'Come here often? Make Google your homepage.'. This message needs to be handled, otherwise it will fail the access to the apps icon.
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class webelements2 {
public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.gecko.driver","C:\\Users\\rpremala003\\Downloads\\geckodriver-v0.14.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com/");
driver.manage().window().maximize();
WebDriverWait wait = new WebDriverWait(driver, 10);
if (driver.findElements(By.xpath("//a[#title='No thanks']")).size() !=0) {
wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath("//a[#title='No thanks']"))));
driver.findElement(By.xpath("//a[#title='No thanks']")).click();
}
driver.findElement(By.xpath("//div[#id='gb']//div[#id='gbwa']/div/a[#title='Google apps' and #role='button']")).click();
wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath("//a[#id='gb8']/span[text()='Maps']"))));
driver.findElement(By.xpath("//a[#id='gb8']/span[text()='Maps']")).click();
}
}
Try this code and let me know, if you have further queries.
I've tested the code below and it works.
driver.get("https://www.google.com");
driver.findElement(By.cssSelector("a[title='Google apps']")).click();
new WebDriverWait(driver, 3).until(ExpectedConditions.elementToBeClickable(By.id("gb8"))).click();
You basically need to click the Apps icon, wait for the Maps icon to be clickable, and click it.
I'm assuming you don't work for Google so in that case, why test the UI? You can just directly navigate to https://maps.google.com and skip the UI clicks.
How about this ... worked for me.
driver.get("https://www.google.com/");
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[#id='gbwa']//a[#title='Google apps']"))));
driver.findElement(By.xpath(".//*[#id='gbwa']//a[#title='Google apps']")).click();
//click map icon
driver.findElement(By.cssselector("a#gb8")).click();
Use this, working for me.
Use Chrome driver instead of Firefox driver.
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class GoogleMaps {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://www.google.com/");
driver.manage().window().maximize();
driver.findElement(By.xpath("//*[#id='gbwa']")).click();
driver.findElement(By.xpath("//li/a[#id='gb8']")).click();
Thread.sleep(6000);
driver.quit();
}
}
Output :

Cannot automated sign in for dropbox.com through Selenium (Java)

Hello I am new to automation and i have tried automating dropbox.com sign in but my code is failing after clicking on sign in link. i am not able to pass the values (username and password) in the frame of the sign in box. Following is my code.
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Drop_box {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
WebDriver driver = new FirefoxDriver();
driver.get("http://www.dropbox.com");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.findElement(By.xpath("//*[#id='sign-in']")).click();
driver.findElement(By.xpath("//*[#id='pyxl1851']")).sendKeys("123#gmail.com");
driver.findElement(By.xpath("//*[#id='pyxl1854']")).sendKeys("1234");
driver.findElement(By.xpath("//*[#id='regular-login-forms']/form[1]/div[3]/button")).click();
}
}
This should work:
driver.findElement(By.cssSelector("a#sign-in")).click();
driver.findElement(By.cssSelector("div#index-sign-in-modal input[name='login_email']")).sendKeys("email");
driver.findElement(By.cssSelector("div#index-sign-in-modal input[name='login_password']")).sendKeys("password");
driver.findElement(By.cssSelector("div#index-sign-in-modal div.sign-in-text")).click();
If you prefer xpath over css selectors, then use following lines of code:
driver.findElement(By.xpath("//*[#id='sign-in']")).click();
driver.findElement(By.xpath("//div[#id='index-sign-in-modal']//input[#name='login_email']")).sendKeys("email");
driver.findElement(By.xpath("//div[#id='index-sign-in-modal']//input[#name='login_password']")).sendKeys("password");
driver.findElement(By.xpath("//div[#id='index-sign-in-modal']//div[#class='sign-in-text']")).click();