Can you please let me know how we can logout in chrome browser by using selenium?
e.g
public class AJ {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.get("http://facebook.com");
WebElement element=driver.findElement(By.name("email"));
element.sendKeys("user#example.com");
element=driver.findElement(By.name("pass"));
element.sendKeys("password");
element.submit();
The following code should help you.
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.get("http://facebook.com");
WebElement element=driver.findElement(By.name("email"));
element.sendKeys("user#example.com");
element=driver.findElement(By.name("pass"));
element.sendKeys("password");
element.submit();
//Click on dropdown menu then logout button
driver.findElement(By.id("userNavigationLabel")).click();
driver.findElement(By.id("logout_form")).click();
//Check to see if email login box is available
//therefore confirming user has logged out
driver.findElement(By.name("email"));
}
I recommend using the Chrome Developer tools to help you find unique attributes of a page for Selenium to find.
I hope this helps!
In python the same is done using this line of code. It uses the same module i.e, Selenium.
So just change the element using css selector by using the argument passed below.
logout1 = driver.find_element_by_css_selector("._w0d[action='https://www.facebook.com/logout.php?button_name=logout&button_location=settings']").submit()
Hope it works.
I am able to successfully logout from Facebook.
Here is the Java code
String url = "http://facebook.com";
String email = "email";
String password = "password";
System.setProperty("webdriver.chrome.driver", "src/chromedriver 3");
WebDriver driver = new ChromeDriver();
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-notifications");
driver = new ChromeDriver(options);
driver.get(url);
driver.manage().window().maximize();
driver.findElement(By.id("email")).sendKeys("Your email here");
driver.findElement(By.id("pass")).sendKeys("Your password here" + Keys.ENTER);
driver.findElement(By.id("logoutMenu")).click();
Thread.sleep(2000);
driver.findElement(By.xpath("//form[contains(#id,'show_me_how_logout')]/../../../..")).click();
Related
Using latest Selenium -server-4.7.0 jar and latest jdk , selenium is frequently giving no such element found error.
URL
Sample Code:
public static void main(String[] args) throws InterruptedException
{
System.setProperty(
"webdriver.chrome.driver",
"C:\\Users\\USER\\Downloads\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://msqabaseappsa.z13.web.core.windows.net/#/");
WebElement b = driver.findElement(By.xpath("//button/span[text()='Microsoft Sign In']"));
b.click();
System.out.print("Click Succeess");
driver.close();
}
Problem : Need to click on "Microsoft Sign In"
Add some wait:
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(15));
driver.get("https://msqabaseappsa.z13.web.core.windows.net/#/");
WebElement b = driver.findElement(By.xpath(".//*[#class='btn btn-success btn-block']"));
b.click();
*** Updated the question with relevant html code.
I'm facing error while trying to select any value from dropdown.
The error is
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to find element with css selector == #oHeight
I have already set all the IE settings as mentioned in Selenium Docs
The code i have tried is mentioned below:
System.setProperty("webdriver.ie.driver", "D:\\Workspace\\Selenium\\Model\\servers\\IEDriverServer_32bit.exe");
WebDriver driver = new InternetExplorerDriver();
driver.manage().timeouts().implicitlyWait(15000, TimeUnit.MILLISECONDS);
driver.get("http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/showModalDialog2.htm");
WebElement ddlHeight = driver.findElement(By.id("oHeight"));
Select select = new Select(ddlHeight);
select.selectByVisibleText("150");
driver.findElement(By.xpath("//input[#value='Push To Create']")).click();
driver.quit();
The system config is Windows 7 + IE 11
You can try this code , It's working on my System :
I'm using IE 11 + Win7 Professional:
Code
public class Sandeep {
static WebDriver driver;
static WebDriverWait wait;
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.ie.driver", "D:\\Automation\\IEDriverServer.exe");
InternetExplorerOptions options = new InternetExplorerOptions();
options.ignoreZoomSettings();
driver = new InternetExplorerDriver(options);
driver.manage().window().maximize();
wait = new WebDriverWait(driver, 40);
driver.get("http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/showModalDialog2.htm");
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("oHeight")));
wait.until(ExpectedConditions.elementToBeClickable(By.id("oHeight")));
Select select = new Select(driver.findElement(By.id("oHeight")));
select.selectByVisibleText("150");
// wait.until(ExpectedConditions.elementToBeClickable(By.name("Push To Create")));
// driver.findElement(By.name("Push To Create")).click();
driver.close();
}
}
Please let me know if you have any concerns related to this.
I am writing selenium script and It's working perfect but when code is running then my browser is automatically closed?
public static void main(String args[])
{
System.setProperty("webdriver.chrome.driver",
"./chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("URL");
String email = "EMAil";
String password = "123";
int ELEMENT_WAIT_TIME_SEC = 60;
WebDriverWait explicitWait = new WebDriverWait(driver, ELEMENT_WAIT_TIME_SEC);
String locator = "//input[#type='email'][#aria-label='Email']";
By findBy = By.xpath(locator);
WebElement Login = explicitWait.until(ExpectedConditions.elementToBeClickable(findBy));
Login.click();
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("arguments[0].setAttribute('aria-invalid',true);", Login);
Login.sendKeys(email);
String plocator = "//input[#type='password'][#aria-label='Password']";
By findByp = By.xpath(plocator);
WebElement Password = explicitWait.until(ExpectedConditions.elementToBeClickable(findByp));
Password.click();
jse.executeScript("arguments[0].setAttribute('aria-invalid',true);", Password);
Password.sendKeys(password);
}
Check your code. May be you used driver.close() method to close the browser.
Just comment or delete that code and the browser will not close automatically.
I got over this error by using the Code Runner extension of VS Code instead of manually running node file-path.
Works well.
This is a case of browser version compatibility issue with chrome. so update your chromedriver.exe in sync with current version of Chrome browser.
I am new to Selenium webDriver. Below is the code which i used to select day, Month & Year from dropdown of Facebook.
public class Facebook {
public static void main(String[] args) throws InterruptedException{
System.setProperty("webdriver.gecko.driver", "E:/Selenium/geckodriver-v0.11.1-win64/geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.facebook.com/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.id("u_0_1")).sendKeys("selenium");
driver.findElement(By.id("u_0_3")).sendKeys("selenium");
driver.findElement(By.id("u_0_5")).sendKeys("1234567891");
driver.findElement(By.id("u_0_8")).sendKeys("1234567891");
Thread.sleep(500);
new Select(driver.findElement(By.xpath(".//*[#id='day']"))).selectByVisibleText("17");
Thread.sleep(500);
new Select(driver.findElement(By.id("month"))).selectByVisibleText("Aug");
new Select(driver.findElement(By.id("year"))).selectByVisibleText("1988");
driver.findElement(By.id("u_0_i")).click();
driver.findElement(By.id("u_0_e"));
Not able to select dropdown list. Please help me in this.
Thanks in advance. :)
I can only provide a lead since you have not captured all necessary details in code snippet.
WebElement selElem=driver.findElement(By.xpath(".//*[#id='day']")));
Select sel=new Select(selElem);
sel.selectByVisibleText("Aug");
You can use this code and it will work for you:
System.setProperty("webdriver.gecko.driver", "E:/Selenium/geckodriver-v0.11.1-win64/geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://www.facebook.com/");
driver.findElement(By.id("u_0_1")).sendKeys("selenium");
driver.findElement(By.id("u_0_3")).sendKeys("selenium");
driver.findElement(By.id("u_0_5")).sendKeys("1234567891");
driver.findElement(By.id("u_0_8")).sendKeys("1234567891");
new Select(driver.findElement(By.id("day"))).selectByVisibleText("17");
new Select(driver.findElement(By.id("month"))).selectByVisibleText("Aug");
new Select(driver.findElement(By.id("year"))).selectByVisibleText("1988");
driver.findElement(By.id("u_0_i")).click();
You have done a mistake in your code i.e. if you are using an implicit wait then you have to write it before your url launch. In your case you wrote it after url launch. Now copy and paste this code and it will definitely work.
Try this below code.
I have simply increase the wait after selecting the each value from dropdown selection.
Note:- If id is present inside the element, then use id locator, instead of using xpath locator.
new Select(driver.findElement(By.id("day"))).selectByVisibleText("17");
Thread.sleep(1000);
new Select(driver.findElement(By.id("month"))).selectByVisibleText("Aug");
Thread.sleep(1000);
new Select(driver.findElement(By.id("year"))).selectByVisibleText("1988");
I am new to Selenium. I am learning by automating some test scenarios on MakeMyTrip website.
Scenario: Editing the user account created.
Code:(yet to be completed)
public class AccountEdit {
#Test
public void AccEdit()
{
WebDriver driver = new FirefoxDriver();
driver.get("http://www.makemytrip.com/");
driver.manage().window().maximize();
driver.findElement(By.id("ssologinlink")).click();
driver.findElement(By.id("username")).sendKeys("abcd#gmail.com");
driver.findElement(By.id("password_text")).sendKeys("*****");
driver.findElement(By.id("login_btn")).click();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(By.id("ssologinlink")).click(); **======> Here I notice the click is not happening to select the My Account or My Profile from the drop down.**
}
}
Kindly let me know how I can take the focus back to the webelement once I login.
driver.findElement(By.id("ssologinlink")).click();
works fine the first time but not post the user login.
Thank you for your comments. The element ID had not changed post the login. I had to wait for the user name to appear before I click on the drop down.
Below is the code which worked for me:
public class AccountEdit {
#Test
public void AccEdit()
{
WebDriver driver = new FirefoxDriver();
driver.get("http://www.makemytrip.com/");
driver.manage().window().maximize();
driver.findElement(By.id("ssologinlink")).click();
driver.findElement(By.id("username")).sendKeys(""abcd#gmail.com"");
driver.findElement(By.id("password_text")).sendKeys("*******!");
driver.findElement(By.id("login_btn")).click();
WebElement myDynamicElement = (new WebDriverWait(driver, 10)).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[#id='ssologinlink']/strong[contains(text(),'user')]")));
myDynamicElement.click();
}
}
Try waiting for the element to be clickable with Expected Conditions of Explicit waits. See the doc here
public class AccountEdit {
#Test
public void AccEdit()
{
WebDriver driver = new FirefoxDriver();
driver.get("http://www.makemytrip.com/");
driver.manage().window().maximize();
driver.findElement(By.id("ssologinlink")).click();
driver.findElement(By.id("username")).sendKeys("abcd#gmail.com");
driver.findElement(By.id("password_text")).sendKeys("*****");
driver.findElement(By.id("login_btn")).click();
//Waiting for the element to be clickable with Explicit wait
WebElement myDynamicElement = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.elementToBeClickable(By.id("ssologinlink")));
myDynamicElement.click();
}
}
Some time element ID gets changed post login(something like dynamic ID).. pls. check the element ID again and update..