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()
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'))
I am new here and I tried to write a code that should go through all of die 3090 pages and extract the information given in the table (box-b).
But it doesn't work. It seems to stay on the first page and it extracts those informations over and over.
If any of you has an idea how I can fix it please help me :)
Lara
import requests
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
import time
url = "https://www.dieversicherer.de/versicherer/auto---reise/typklassenabfrage#orderBy=kh&orderDirection=ASC"
driver = webdriver.Chrome('/Users/laraschneider/Downloads/chromedriver')
driver.get(url)
time.sleep(5)
html = driver.page_source
soup = BeautifulSoup(html, "html.parser")
number = 0
while number < 3090:
results_html = soup.find_all('div', "ttw-entry__box-b")
results = list()
for result in results_html:
results.append(result.text)
for t in results:
print(t)
time.sleep(5)
element = driver.find_element_by_xpath("/html/body/main/div/div[5]/div/div/div/div[2]/div[2]/div[2]/div[2]/button[2]/div")
action = ActionChains(driver)
action.move_to_element(element)
action.click(element).perform()
number = number+1
Use driver.execute_script("arguments[0].click();", element) instead of ActionChains. Here is the full code:
import requests
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
url = "https://www.dieversicherer.de/versicherer/auto---reise/typklassenabfrage#orderBy=kh&orderDirection=ASC"
driver = webdriver.Chrome('/Users/laraschneider/Downloads/chromedriver')
driver.get(url)
time.sleep(5)
html = driver.page_source
soup = BeautifulSoup(html, "html.parser")
number = 0
while number < 3090:
results_html = soup.find_all('div', "ttw-entry__box-b")
results = list()
for result in results_html:
results.append(result.text)
for t in results:
print(t)
time.sleep(5)
element = driver.find_element_by_xpath('/html/body/main/div/div[5]/div/div/div/div[2]/div[2]/div[2]/div[2]/button[2]/div')
driver.execute_script("arguments[0].click();", element)
number = number+1
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");
}