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'))
Related
I'm writing a code with selenium.
My code should open YouTube, input a word, click on search button and open a video.
Everything except the last one. I can't open a video. Could you please help me?
My code:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome('/Users/mariabiriulina/Desktop/chromedriver')
driver.get('https://youtube.com/')
searchbox = driver.find_element(By.XPATH, '//input[#id="search"]')
searchbox.click()
searchbox.send_keys('Justin Timberlake')
searchbutton = driver.find_element(By.XPATH, '//*[#id="search-icon-legacy"]')
searchbutton.click()
elements = driver.find_element(By.XPATH, '//*[#id="video-title"]/yt-formatted-string').click()
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome('/Users/mariabiriulina/Desktop/chromedriver')
wait = WebDriverWait(driver, 20)
driver.get('https://youtube.com/')
searchbox = driver.find_element(By.XPATH, '//input[#id="search"]')
searchbox.click()
searchbox.send_keys('Justin Timberlake')
searchbutton = driver.find_element(By.XPATH, '//*[#id="search-icon-legacy"]')
searchbutton.click()
video_titles_xpath = "//*[#id='video-title']/yt-formatted-string"
wait.until(EC.visibility_of_element_located((By.XPATH, video_titles_xpath)))
video_titles = driver.find_elements(By.XPATH, video_titles_xpath)
video_titles[0].click()
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.
If any one logged inside any application using chrome browser,notification pop-up appears to save password/Allow notification.
How to handle this notification pop-up through selenium web-driver? Sometimes two pop-ups appears(one is for save password and another one is to allow notification).I have already tried to handle using Alert class but could not succeeded.kindly help me on this.
You can open use ChromeOptions Class. Below is the sample code.
ChromeOptions chrome_Profile = new ChromeOptions();
chrome_Profile.addArguments("chrome.switches","--disable-extensions");
chrome_Profile.addArguments("--disable-save-password");
chrome_Profile.addArguments("disable-infobars");
System.setProperty("webdriver.chrome.driver","c/chromedriver.exe");
//Passing chrome_Profile while initializing the ChromeDriver
WebDriver driver = new ChromeDriver(chrome_Profile);
Let me know if this helps.
You can use below code to allow chrome to send notifications:
ChromeOptions options=new ChromeOptions();
Map<String, Object> prefs=new HashMap<String,Object>();
prefs.put("profile.default_content_setting_values.notifications", 1);
//1-Allow, 2-Block, 0-default
options.setExperimentalOption("prefs",prefs);
ChromeDriver driver=new ChromeDriver(options);
#Pritesh patel
it didnt work for me.. I want to allow the flash to run
package com.selenium.Basics;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
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.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import Flash.FlashObjectWebDriver;
public class GuruPgm14FlashTesting {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
String driverPath="C:\\selenium\\chromedriver.exe";
System.setProperty("webdriver.chrome.driver", driverPath);
ChromeOptions options=new ChromeOptions();
Map<String, Object> prefs=new HashMap<String,Object>();
prefs.put("profile.default_content_setting_values.notifications", 1);
//1-Allow, 2-Block, 0-default
options.setExperimentalOption("prefs",prefs);
WebDriver driver=new ChromeDriver(options);
driver.get("http://demo.guru99.com/test/flash-testing.html");
Thread.sleep(5000);
driver.findElement(By.xpath("//embed[#play='false']")).click();
System.out.println("Clicked to allow the addon");
Thread.sleep(5000);
FlashObjectWebDriver fdriver= new FlashObjectWebDriver(driver, "myFlashVideo");
fdriver.callFlashObject("Play");
Thread.sleep(5000);
fdriver.callFlashObject("StopPlay");
Thread.sleep(5000);
fdriver.callFlashObject("SetVariable","/:message","Flash testing using selenium Webdriver");
System.out.println(fdriver.callFlashObject("GetVariable","/:message"));
}
}
const webdriver = require('selenium-webdriver');
var chromeCapabilities = webdriver.Capabilities.chrome();
var chromeOptions = {
'args': ['--disable-notifications']
};
chromeCapabilities.set('chromeOptions', chromeOptions);
If you are using js. Especially when website adding now option to provide add on your screen or even the www dot come want to send you notification, this ultimately makes your windows blackened and inaccessible to element in it.
Try to put this on a different JS file if u do. Cause export is a function
in Python:
if browser == "chrome":
chrome_options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.notifications": 2}
chrome_options.add_experimental_option("prefs", prefs)
self.driver = webdriver.Chrome(chrome_options=chrome_options)
self.driver.maximize_window()
self.driver.implicitly_wait(10)
You can use ChromeOptions class to allow / disable notification popup.
Please find code below :
Map<String, Object> prefs = new HashMap<String, Object>();
// 1 for allowing, 2 for disabling popup
prefs.put("profile.default_content_setting_values.notifications", 1);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", prefs);
WebDriver driver = new ChromeDriver(options);
We can disable all type chrome notifications such as save password, allow locations..., just by adding only one argument.
ChromeOptions ops = new ChromeOptions();
ops.addArguments("--disable-notifications");
System.setProperty("webdriver.chrome.driver", ""c/chromedriver.exe"");
WebDriver driver = new ChromeDriver(ops);
hope this helps you.
Thanks.
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");
}