problem using python to find_element_by_xpath - selenium-webdriver

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

Related

How to make a click on button with selenium?

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()

Trying to check if value has been updated in Selenium

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'))

How to click a button from Browser with Selenium

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();

Unable to locate nCino element through Selenium in Salesforce

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.

How to use WebDriver Wait in IE?

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");
}

Resources