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");
Related
[enter image description here][1]
please resolve issue , please update the same
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(());
driver.get("https://accounts.google.com/signin");
driver.manage().window().maximize();
You can use like this :
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("https://accounts.google.com/signin");
driver.manage().window().maximize();
If you are using selenium 4 try this:
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.manage().timeouts().scriptTimeout(Duration.ofSeconds(30));
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(60));
Deprecated
It means the method implicitlyWait(long, TimeUnit) is no more supported and you should use an updated method to serve the same purpose.
You can change your code to
long time = 2000;
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(time));
Instead of arguments (long, TimeUnit), this method now takes arguments as Duration.ofSeconds(time)
I can't find the error with my xpath element locator
Login using automated testing tool Selenium webdriver
driver.findElement(By.className("btn-primary")).click();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(By.xpath("//input[#aria-labelledby='vaadin-text-field-label-19']")).click();
driver.findElement(By.xpath("//input[#aria-labelledby='vaadin-text-field-label-19']")).sendKeys("xxx#gmail.com");
driver.findElement(By.xpath("//input[#type='password']")).click();
driver.findElement(By.xpath("//input[#type='password']")).sendKeys("12345678");
driver.findElement(By.className("btn-block")).click();
Try the following xpath to enter details.
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://jbm4u.com/jupiter/#/signin");
driver.findElement(By.className("btn-primary")).click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.xpath("//div[#class='card']//vaadin-text-field[#placeholder='Enter email address']")).click();
driver.findElement(By.xpath("//div[#class='card']//vaadin-text-field[#placeholder='Enter email address']")).sendKeys("xxx#gmail.com");
driver.findElement(By.xpath("//div[#class='card']//vaadin-password-field[#placeholder='Enter Password']")).click();
driver.findElement(By.xpath("//div[#class='card']//vaadin-password-field[#placeholder='Enter Password']")).sendKeys("12345678");
}
Browser Snapshot
Can you please try with this xpath
driver.findElement(By.xpath("//input[#placeholder='Enter email address']")).click();
driver.findElement(By.xpath("//input[#placeholder='Enter email address']")).sendKeys("xxx#gmail.com");
Please avoid referring to attributes which has dynamic indexing in your example(vaadin-text-field-label-21) 21 is dynamically generated which tend to change for every time when you refresh or for a new session.
If you're wanting to fill in the email address you need this selector:
driver.findElement(By.cssSelector("vaadin-text-field[placeholder='Enter email address']")).sendKeys("test#email.com")
The password field is:
driver.findElement(By.cssSelector("vaadin-password-field[placeholder='Enter Password']")).sendKeys("dwedewdw")
And the submit button:
driver.findElement(By.cssSelector("button[class*='btn-block']")).click()
Try and avoid Xpath whenever possible, it's too flakey. I've tested the above selectors myself.
how is possible to find the xpath for an angularJs element? for instance i discovered that all links in my page have the same xpath due to the repeated items in angularJs -->
.//*[#id='div_1_1_1_2']/div/div[1]/div[2]/div[2]/div/div[2]/div[1]/div/div[2]/a
but i have 10 of element , they are differente for text, so i tried with `"
so i tried with contains but it never find it
.//[#id='div_1_1_1_2']/div/div[1]/div[2]/div[2]/div/div[2]/div[1]/div/div[2]/a[contains(#aria-label='Creazione Prodotto')]"`
i use selenium, junit4 , firefox webDriver
this is my code
public class PB01_TTT {
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
WebElement element;
#Before()
public void setUp() throws Exception {
FirefoxProfile fxProfile = new FirefoxProfile();
fxProfile.setPreference("browser.download.folderList", 2);
fxProfile.setPreference("browser.download.manager.showWhenStarting", false);
fxProfile.setPreference("browser.helperApps.alwaysAsk.force", false);
fxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk",
"application/pdf, application/x-pdf, application/octet-stream");
fxProfile.setPreference("pdfjs.disabled", true);
driver = new FirefoxDriver(fxProfile);
baseUrl = "https://w8aon2bpm.replynet.prv:9443";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
#Test
public void testPBO1TTT() throws Exception {
driver.get(baseUrl + "/ProcessPortal/login.jsp");
// driver.get(baseUrl + "/ProcessPortal/dashboards/SYSRP/RESPONSIVE_WORK");
driver.findElement(By.id("username")).clear();
driver.findElement(By.id("username")).sendKeys("user");
driver.findElement(By.id("password")).clear();
driver.findElement(By.id("password")).sendKeys("password");
String columnToDisplay=driver.findElements(By.xpath(".//*[#id='div_1_1_1_2']/div/div[1]/div[2]/div[2]/div/div[2]/div[1]/div/div[2]/a[contains(#aria-label='Creazione Prodotto')]"));
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
element = (WebElement) driver.findElements(By.xpath(columnToDisplayXpath));
Assert.assertNotNull(element);
it always return me a notFoundElement, any suggestions ?
Thank you
If you have 10 links, then there is a high chance they are different in a way even if the path is the same, in this case you need to construct an path based on the thing that is different.
For example: use href, text or other any part that differs
//a[contains(#href, 'part_of_href')]
//a[contains(text(), 'part_of_text')]
//a[#title='title']
//a[contains(#aria-label='Creazione Prodotto')]
If you need any help in getting the selector please add the html section of the links, you can change the url if needed.
Tip: avoid using absolute xpaths and attributes that do not suggest anything and they can change like: .//*[#id='div_1_1_1_2']/div/div[1]/div[2]/div[2]/div/div[2]/div[1]/div/div[2]/a[contains(#aria-label='Creazione Prodotto')]"
This will give you a lot of work in the future.
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..
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();