I would like to click on the "OK" button with the selenium
Button generated from browser
These are the JavaScript alerts. Selenium has alert class to handle them. Refer below code:
#python webdriver code
from selenium import webdriver
browser = webdriver.Firefox()
browser.get("http://sandbox.dev/alert.html")
alert = browser.switch_to_alert()
alert.accept() #this line accepts the alert
browser.close()
Java Webdriver code to accept alert:
import org.openqa.selenium.Alert; //import this class
Alert alert = driver.switchTo().alert();
alert.accept();
Related
I am trying to click the next page and then get the page number....It should come back 2 and I always get 1 so it doesnt look like it is waiting???
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import requests
driver = webdriver.Chrome()
driver.get('https://cases.ra.kroll.com/claires/Home-DocketInfo')
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '(//*[#id="p-next-page"])[1]'))).click()
WebDriverWait(driver, 10).until(EC._element_if_visible((By.XPATH, '//*[#id="pagenum"]')))
print(driver.find_element(By.XPATH,'//*[#id="pagenum"]').get_attribute('value'))
source code on webpage
<a class="btn btn-mini" href="#" onclick="select_all_contacts();; return false;">Select all /a>
Using selenium to click button as follows using python on Rpi
driver.find_element_by_xpath("//*[contains(#onclick,'select_all_contacts()')]").click()
However get error message 'no such element' - 'unable to locate element .....'
What am I doing wrong
Possibly you are missing a wait / delay before accessing this element.
Try this:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome(executable_path='chromedriver.exe')
wait = WebDriverWait(driver, 20)
wait.until(EC.visibility_of_element_located((By.XPATH, "//*[contains(#onclick,'select_all_contacts()')]"))).click()
Alternatively you can try
wait.until(EC.visibility_of_element_located((By.XPATH, "//*[contains(text(),'Select all')]"))).click()
Also possible the element is inside an iframe.
In this case you will have to swith to the iframe before accessing this element
I am using nCino application (build on Salesforce) for my project. nCino is a framework built on Salesforce application and called in a .
I am trying to automate functional flows in nCino UI through Selenium Java in Chrome browser (tried in Firefox too)
Issue 1: Switch to iframe and click on element
Refer to iframe code below
Console error:
Selenium Code Snippet to switch to iframe and click element inside element:
package nCInoAutomation;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.apache.http.Header;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.html5.ApplicationCache;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Main {
public static void main(String args[]) throws InterruptedException {
//System.setProperty("webdriver.gecko.driver", Parameters.FIREDRIVER);
System.setProperty("webdriver.chrome.driver", Parameters.CHROMEDRIVER);
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--start-maximized");
// chromeOptions.addArguments("--disable-web-security");
WebDriver driver = new ChromeDriver(chromeOptions);
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
//WebDriver driver = new FirefoxDriver();
driver.get(Parameters.APPURL);
Thread.sleep(2000);
WebElement formElement = driver.findElement(By.id("login_form"));
formElement.findElement(By.id(Parameters.UNAME_XPATH)).sendKeys(Parameters.UNAME);
formElement.findElement(By.id(Parameters.PWD_XPATH)).sendKeys(Parameters.PWD);
formElement.findElement(By.id(Parameters.ENTER_XPATH)).sendKeys(Keys.ENTER);
Thread.sleep(20000);
System.out.println("Wait Over");
WebDriverWait wait=new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(2));
List<WebElement> list = driver.findElements(By.tagName("iframe"));
System.out.println(list.size());
JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement ele = list.get(2);
js.executeScript("arguments[0].setAttribute('style', 'background: yellow; border: 2px solid red;');", ele);
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollBy(0,500)", "");
System.out.println("********Switched to the iframe*******");
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[#id='ncSecondaryNavigation']//span[text()='Loan'])[2]")));
System.out.println("Clicked");
}
}
It says that it has switched to iframe but doesnt look like so.
Issue 2: Unable to locate element
Also, iframe contains most of the angular js elements hence Unable to identify elements. See screenshot below:
Application looks somewhat like below:
Can you please help me automate below:
1. Switch to iframe once landed on Salesforcw screen.
2. Navigate to 'Collateral' screen by clicking on the link.
I have few fields along with text field ,date, rich text editor, dropdown and browse button to upload image. I am getting error as unable to locate element on selecting the dropdown after rich text editor. Here is my code:
package newpackage;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.*;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;
public class MyClass {
public static void main(String args[])throws NoSuchElementException,InterruptedException {
System.setProperty("webdriver.gecko.driver", "C:\\\\geckodriver-v0.18.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("url");
driver.findElement(By.id("area_number")).sendKeys("1221");
driver.findElement(By.id("street_name")).sendKeys("abc");
driver.findElement(By.id("email_of_owner")).sendKeys("test#gmail.com");
driver.findElement(By.id("name_of_contact")).sendKeys("Test Papri");
driver.findElement(By.id("contact_date")).click();
driver.findElement(By.xpath("/html/body/div[4]/div/div[2]/button[4]")).click();
WebDriverWait wait = new WebDriverWait(driver,20);
wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.tagName("iframe"))));
//Code for Rich text editor goes here//
jsExecutor.executeScript("window.scrollTo(0, document.body.scrollHeight)");
//Active value is being displayed in a li tag as selected after a button is clicked so tried with this.
WebDriverWait wait1 = new WebDriverWait(driver,30);
wait1.until(ExpectedConditions.elementToBeSelected(By.xpath("/html/body/section[2]/div/div[2]/div/div/div/div[2]/div[1]/form/div[7]/div[2]/div/div/div/div/ul/li[2]/a/span[1]")));
//Select dropdown is a button so tried to click it.This is the xpath of the button
WebElement dropdown = driver.findElement(By.xpath("/html/body/section[2]/div/div[2]/div/div/div/div[2]/div[1]/form/div[7]/div[2]/div/div/div/button"));
//dropdown.click();
//driver.close();
}
}
I have Home Page, from the if I click Login, it will be navigated to another page (there i have to enter login credentials to login the account.
Without the below piece of Code, it is perfectly working for Firefox and Chrome.. But IE it is not working.. I assumed to add wait so that IE problem will resolve..
WebDriverWait Test_Wait=new WebDriverWait(driver,10);
WebElement click=Test_Wait.until(ExpectedConditions.elementIfVisible("login_xpath"));
I have added this code.. But I am getting error to change elementIfVisible to elementClickable.. If i change to elementClickable. Again it is giving error to change elementIfVisible.. How to resolve this??
package com.test.testCase;
import java.io.File;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import com.test.utitlity.clickEvent;
import com.test.utitlity.globalVariables;
import com.test.utitlity.switchWindow;
public class driverManager extends globalVariables{
public static void driverManager(){
browser="ie";
if(browser.equals("ie")){
File file = new File("./IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getPath());
driver= new InternetExplorerDriver();
}
else if (browser.equals("firefox"))
{
driver=new FirefoxDriver();
}
else if (browser.equals("chrome"))
{
File file= new File("./chromedriver.exe");
System.setProperty("webdriver.chrome.driver", file.getPath());
System.out.println(file.getPath());
driver=new ChromeDriver();
}
driver.get("http://www.xxxx.in");
clickEvent.clickAt("login_xpath");
WebDriverWait Test_Wait=new WebDriverWait(driver,10);
WebElement click=Test_Wait.until(ExpectedConditions.elementIfVisible("login_xpath"));
switchWindow.swindow("TST.");
}
}
From the code as I can see, you are using it incorrectly. First, you have to wait for the element to be visible and then you have to click on it but you are using it the other way around, and hence it is not able to find the login button as it has been clicked and you are in the new page already.
Please use the wait like this. This will work in all browsers (IE, always a hindrance, but it will work nevertheless):
try{
WebDriverWait Test_Wait=new WebDriverWait(driver,20);
WebElement click=Test_Wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("login_xpath")));
clickEvent.clickAt("login_xpath");
}catch(Throwable e){
System.err.println("The login element is not found");
}