There is a scenario where I get the modal dialog box when I tried to login.
I am not able to handle login scenario because webdriver is not getting access to the modal dialog box. Is there any way to handle this?
See the details in below image:
public class TwitterLogin
{
static WebDriver driver;
static String baseUrl="myURL";
public static void main(String[] args)
{
driver= new FirefoxDriver();
driver.get(baseUrl);
driver.manage().window().maximize();
//******Click on Login Link**********//
WebElement LoginButton= driver.findElement(By.xpath("//button[#class='Button StreamsLogin js-login']"));
LoginButton.click();
WebElement UsernameTextbox= driver.findElement(By.xpath("//input[contains(#name,'email]')]"));
UsernameTextbox.clear();
UsernameTextbox.sendKeys("uName");
WebElement PasswdTextbox=driver.findElement(By.xpath("//input[contains(#type,'password')]"));
PasswdTextbox.clear();
PasswdTextbox.sendKeys("1234");
}
}
After clicking on Login button, I'm not able to enter username & password.
just write the following code after click on login button:
Thread.sleep(3000); //i use here wait implicit wait. Try to use explicit wait here.
driver.findElement(By.cssSelector("a.Icon.Icon--close.Icon--medium.dismiss")).click();
In Python , work with Selenium 4 and later versions:
copy selector path
wait to open new dialog
time.sleep(5)
phone_path='div:nth-child(4) > span > div:nth-child(3) > button'
log_in_phone = driver.find_element(By.CSS_SELECTOR, phone_path)
log_in_phone.click()
Related
We have used JS click and normal click but it's moving to the footer.
public static void jsClick(WebDriver driver, WebElement element,String msg) {
try {
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", element);
WaitUtils.staticWait(5000); //3000
LoggerHelper.log().info(msg);
} catch (Exception e) {
LoggerHelper.log().error("Not able to click", e.getCause());
}
}
You don't click on a button in Selenium by injecting as JavaScript into it. You should use the Selenium driver in order to click:
element = driver.findElement(By.CSS, 'button[id="ButtonID"]')
element.click()
In you code example you are not using the Selenium plugin.
While clicking on button which is outside form tag using submit() of selenium webdriver getting org.openqa.selenium.JavascriptException instead of NoSuchElementException(as per the documentation) If the given element is not within a form
following is the code
public void setup() {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
driver.manage().window().maximize();
System.out.println("Execution Starts");
// Setting chrome driver property and opening chrome browser
System.out.println("Browser opened.");
// We need to downcast WebDriver reference varaible to use JavascriptExecutor methods
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
// identify elements
driver.get("https://www.youtube.com/");
// submitting form with click()
driver.findElement(By.xpath("//ytd-button-renderer[#class='style-scope ytd-masthead style-default size-default']//yt-icon[#class='style-scope ytd-button-renderer']")).submit();
}
I have created a Selenium Full page screenshot test for Responsive tests using selenium.
But if I am going to run against the SharePoint Site then it's not taking a full-page screenshot but If I'll give any particular public site to capture the screenshots.
Here is the code snippet:
public class resTest {
#Test(groups = {"Test1"})
public void ResTest() throws InterruptedException, IOException {
System.setProperty("webdriver.gecko.driver", "C:\\browserdriver\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get(getUrl());
driver.manage().window().maximize();
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(30));
//Enter email ID
WebElement enterEmail = wait.until(ExpectedConditions.elementToBeClickable(By.name("loginfmt")));
enterEmail.sendKeys(getUsername());
WebElement clickNext = wait.until(ExpectedConditions.elementToBeClickable(By.className("win-button")));
clickNext.click();
//Enter Password
WebElement SubmitPass = wait.until(ExpectedConditions.elementToBeClickable(By.name("passwd")));
SubmitPass.sendKeys(getPassword());
//Click on Sign In button
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#value='Sign in']")));
element.click();
WebElement afterNext = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#value='No']")));
afterNext.click();
Thread.sleep(9000);
File src = ((FirefoxDriver)driver).getFullPageScreenshotAs(OutputType.FILE);
FileHandler.copy(src, new File(System.getProperty("user.dir")+"/Res2/screen.png"));
}
}
On a headless browser I use the following to get a screen of the full page
full_width = driver.execute_script('return document.body.parentNode.scrollWidth')
full_height = driver.execute_script('return document.body.parentNode.scrollHeight')
driver.set_window_size(full_width, full_height)
driver.save_screenshot(path)
The advantage of a headless browser window is that you can resize it to whatever size you want, even if it is larger than your screen resolution. For example, if your screen is 1920x1080 and you resize the headless browser window to 1000x5000 and take a screenshot, then the image will be 1000x5000 pixels.
I have this code show below:
driver.findElement(By.id("submit")).sendKeys(Keys.ENTER);
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.findElement(By.id("search-trigger")).sendKeys(Keys.ENTER);
driver.findElement(By.id("search-trigger")).sendKeys("Shampoo");
driver.findElement(By.id("search-trigger")).sendKeys(Keys.ENTER);
I want to search for a product and search option is before and after login page but here i want to do it after login page
I have used
driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
but it doesn't work and i cannot use
WebElement “”= driver.findElement(By.id(""));
as the search option is on both pages
and URL remains same after login also
Per my understanding, in short, you want to ensure user has logged in before searching for a product. However u will not be able to use WebElement “”= driver.findElement(By.id(""));
Did you try explicit wait for the presence of username/logout text...etc
WebElement element = wait.until(ExpectedConditions.elementIsVisible(By.id(>someid>)));
Below worked for me, tried searching Samsung after login in flipkart:
public static void f() throws InterruptedException
{
By signin = By.xpath(".//*[#id='container']/div/header/div[1]/div[1]/div/ul/li[9]/a");
Thread.sleep(2000);
By mobile = By.xpath("html/body/div[2]/div/div/div/div/div[2]/div/form/div[1]/input");
By password = By.xpath("html/body/div[2]/div/div/div/div/div[2]/div/form/div[2]/input");
By login = By.xpath("html/body/div[2]/div/div/div/div/div[2]/div/form/div[3]/button");
By myaccount = By.xpath(".//*[#id='container']/div/header/div[1]/div[1]/div/ul/li[8]/a");
By search = By.xpath(".//*[#id='container']/div/header/div[1]/div[2]/div/div/div[2]/form/div/div[1]/div/input");
//geko driver for firefox
System.setProperty("webdriver.gecko.driver", "path of geckodriver.exe");
System.setProperty("webdriver.chrome.driver", "path of chromedriver.exe");
driver = new ChromeDriver();
driver.get("https://www.flipkart.com/");
driver.findElement(signin).click();;
driver.findElement(mobile).sendKeys("xxxxxxxxx");
driver.findElement(password).sendKeys("xxxxxxxx");
driver.findElement(login).click();
WebDriverWait wait = new WebDriverWait (driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(myaccount));
driver.findElement(search).sendKeys("samsung");
}
Am doing automation tesing using selenium, i need help in regarding how to select radio button.If possible help me with selenium java code.
Assuming you have selenium set up its just:
selenium.click('radio button locator');
You may want to look at the selenium javadoc http://release.seleniumhq.org/selenium-remote-control/0.9.2/doc/java/com/thoughtworks/selenium/Selenium.html
click > xpath=(//input[#type='checkbox'])[position()=1]
click > xpath=(//input[#type='checkbox'])[position()=2]
click > xpath=(//input[#type='checkbox'])[position()=3]
click > xpath=(//input[#type='checkbox'])[position()=4]
etc ...
use this commands to select random and any radio button
public class radioExamJavaScr {
public static void main(String[] args) throws IOException {
WebDriver driver = new FirefoxDriver();
EventFiringWebDriver dr = new EventFiringWebDriver(driver);
dr.get("http://www.makemytrip.com/");
dr.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
((JavascriptExecutor)dr).executeScript("document.getElementById('roundtrip_r').click();");
WebElement one_way = (WebElement)((JavascriptExecutor)dr).executeScript("return document.getElementById('oneway_r') ;");
System.out.println(one_way.isSelected());
WebElement round_trip = (WebElement)((JavascriptExecutor)dr).executeScript("return document.getElementById('roundtrip_r') ;");
System.out.println(round_trip.isSelected());
}
}
In the above example I am selecting radio button with "ROUND TRIP" using "JavaScript".
The last four lines are to verify and see whether the expected radio button is selected in the page or not.
NOTE: I am giving simple easy solution to solve a problem (selecting a radio) in the choosen webpage. A better code can be written. (user can write a method to accept radio ID and loop through all the existing radio button to see which one of them is selected).
I use this method:
String radioButtonId = "radioButtonId";
selenium.focus("id=" + radioButtonId);
selenium.click("id=" + radioButtonId, "concreteRadioButtonValue");
What you can do is this:
Create a method with a WebElement return type, and use the Method findElement(). Example:
WebDriver test;
test = new FirefoxDriver();
public static WebElement checkAndGet(By b) throws Exception {
return test.findElement(b);
}
Store the WebElement and use the Method click(). Example:
WebElement radiobutton = checkAndGet(By.xpath("//span[#class='label ng-binding']");
radiobutton.click();
Hope this helps!
Test Scenario : Select Gender(Female) radio button
Steps:
Launch new Browser
Open URL http://toolsqa.wpengine.com/automation-practice-form/
Select the Radio button (female) by Value ‘Female’
Code :
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver(); // Step 1 - Launch Browser
driver.get("http://toolsqa.com/automation-practice-form"); // Step 2 - Open Test URL
List<WebElement> rdBtn_Sex = driver.findElements(By.name("sex")); //
int size = rdBtn_Sex.size();
System.out.println("Total no of radio button :"+size);
for (int i=0; i< size; i++)
{
String sValue = rdBtn_Sex.get(i).getAttribute("value"); // Step 3 - 3. Select the Radio button (female) by Value ‘Female’
System.out.println("Radio button Name "+sValue);
if (sValue.equalsIgnoreCase("Female"))
{
rdBtn_Sex.get(i).click();
}
}
What you should do all the time is provide a locator for every object within a page and then use a method.
Like
driver.findElement(By.xpath(//CLASS[contains(., 'what you are looking for')])).click();