I have simple selenium's class. it works very well. Now i'm interested in, how to do the same sing in console mode. In the other words. I need a result (in the code, if request was succeeded or not.) I don't need to show in a web browser. if everything is well i need a return value, if not another return value (something like true or false);
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
WebDriver driver;
WebElement loginInput;
WebElement passwordInput;
WebElement loginSubmit;
driver = new FirefoxDriver();
driver.get("URL");
loginInput = driver.findElement(By.id("id"));
loginInput.sendKeys("ninotyesh");
passwordInput =driver.findElement(By.id("id"));
passwordInput.sendKeys("key");
loginSubmit = driver.findElement(By.id("id"));
loginSubmit.click();
You might consider running your script in HTMLUnitDriver - see short help about it
And at end of your code I would do check for some element which should be present after successful login and print out TRUE in case the driver finds it.
Related
I have a really simple Selenium WebDriver project in Java where I am using FireFox driver.
My goal is to navigate to Google's page (https://www.google.com) and when prompted to accept
Cookies be able to click on the "I agree"-button to just get rid of it and continue the automation process further. But for some reason I just can't get the browser to locate it.
This is the instruction I am using currently:
package main;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class SeleniumGoogleTest {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com");
WebElement acceptButton = driver.findElement
(By.xpath("/html/body/div/c-wiz/div[2]/div/div/div/div/div[2]/form/div/div[2]"));
}
}
I don't know why the browser can't locate it and activate/enable that part of the page
with neither Implicit wait or Explicit wait. Thread.sleep() method don't seem to be the
solution either in this case.
The only error message I get when running the application is that of "Unable to locate the element".
Is it that you actually can't automate some stuff with Selenium WebDriver or have I misunderstood some important concepts here?
Much grateful for all tips !
you can handle it by update the cookies "CONSENT" delete the old one because the value is "PENDING" and Add it with the below value.
driverManager.driver.manage().deleteCookieNamed ("CONSENT");
driverManager.driver.manage().addCookie(new Cookie("CONSENT","YES+shp.gws-"+LocalDate.now().toString().replace("-","")+"-0-RC2.en+FX+374"));
driverManager.driver.navigate().refresh();
The popup is located on an iFrame, first you have to switch to the iFrame:
driver.switchTo().frame(yourFrame);
after you can find the accept button, and click it:
driver.findElement(By.id("id")).click();
I've used the same solution as Jus, but Chrome had 'I agree' button id changed since then
Here how updated solution should look like
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
import time
driver = webdriver.Chrome('chromedriver.exe')
driver.get('https://google.com/xhtml')
time.sleep(2) # seconds until popup appears
try: # 2 different popups
frame = driver.find_element_by_xpath('//*[#id="cnsw"]/iframe') #<-locating chrome cookies consent frame
driver.switch_to.frame(frame)
driver.find_element_by_xpath('//*[#id="introAgreeButton"]').click()#<-looking for introAgreeButton button, but seems google has changed its name since and it only works in old chrome versions.
except NoSuchElementException:
driver.find_element_by_xpath('//*[#id="L2AGLb"]').click() #<- pay attention to new id.
In case if this id will expire, I recommend you to inspect element yourself like this:
Inspect Element
Click twice 'Inspect element'
Locating 'I agree' button's id
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
import time
driver = webdriver.Chrome('chromedriver.exe')
driver.get('https://google.com/xhtml')
time.sleep(2) # seconds until popup appears
try: # 2 different popups
frame = driver.find_element_by_xpath('//*[#id="cnsw"]/iframe')
driver.switch_to.frame(frame)
driver.find_element_by_xpath('//*[#id="introAgreeButton"]').click()
except NoSuchElementException:
driver.find_element_by_xpath('//*[#id="zV9nZe"]').click()
Any help would be much appreciated.
I want my program to log into indeed.ca (this is working, as long as you enter correct user credentials), navigate to a specific job posting(working), click on the first orange apply button(working), an iframe pops up.
Then I want to click on the blue apply button in iframe that appears. "apply with a different resume?" link (if you are not logged in with indeed you won't see this.)
If you don't have an indeed account, and want to help, just try to click on any link in the pop-up iframe. Example: try to click "Create one now" link
The below code worked 2 weeks ago, but now it seems Indeed.ca made a minor change to site and code is broken
import java.io.IOException;
import java.util.ArrayList;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Test {
//IOException, InterruptedException, NoSuchElementException
public static void main(String[] args) throws IOException, InterruptedException, NoSuchElementException {
System.setProperty("webdriver.gecko.driver", "C:\\Users\\Padoga\\Documents\\geckodriver-v0.18.0-win64\\geckodriver.exe");
try {
FirefoxDriver driver = new FirefoxDriver();
driver.get("https://secure.indeed.com/account/login?service=my&hl=en_CA&co=CA");
driver.findElement(By.xpath("//*[#id=\"signin_email\"]")).sendKeys("youremail#email.com");
driver.findElement(By.xpath("//*[#id=\"signin_password\"]")).sendKeys("password");
driver.findElement(By.xpath("//*[#id=\"loginform\"]/button")).click();
driver.navigate().to("https://ca.indeed.com/viewjob?jk=ff97666702741fef&q=marketing&l=Toronto%2C+ON&tk=1boluh7om5igq9ng&from=web");
// int size = driver.findElements(By.tagName("iframe")).size();
// System.out.println(size);
Thread.sleep(3000);
//click orange "apply now" button
driver.findElement(By.xpath("//*[#id=\"apply-state-picker-container\"]/div[1]/span[1]")).click();
Thread.sleep(3000);
//don't believe this is working now - below used to switch to correct pop-up iframe
driver.switchTo().frame(1);
driver.switchTo().frame(0);
//not working anymore -- click "apply with a different resume?" link
driver.findElement(By.xpath("\"//*[#id=\\\"form_container\\\"]/div[2]/div[1]/div[1]/p/a\"")).click();
//no longer reach below steps
//click on resume "choose file" button and upload resume
driver.findElement(By.id("resume")).sendKeys("C:\\Users\\Padoga\\resumes\\Resume.pdf");
//click blue apply button
driver.findElement(By.id("apply-div")).click();
}
catch(Exception e){
//System.out.println(e.getMessage());
}
// driver.quit();
}
}
You need to switch first to perform any action inside iframe elements
The code to switch to element is :-
driver.switchTo().frame(0);
In above code 0 is a index. so it will swicth control to first iframe present in your DOM. You may need to change the index 1,2,.. so on . There another paramenetrs also to switch to frame without index
Basically, we can switch over the elements in frames using 3 ways.
By Index
By Name
Id By Web Element
Refer below URL for more information
http://toolsqa.com/selenium-webdriver/handling-iframes-using-selenium-webdriver/
https://www.guru99.com/handling-iframes-selenium.html
Hope it will help you :)
Try using below code and lets see if it works for you-
driver.switchTo().frame("page_frame");
driver.findElement(By.xpath("//*[#id="form_container"]/div[2]/div[1]/div[1]/p/a")).click();
update-
driver.switchTo().frame("indeedapply-modal-preload-iframe");
driver.findElement(By.xpath("//*[#id="form_container"]/div[2]/div[1]/div[1]/p/a")).click();
update 2-
I found an iframe inside another iframe. So, first you need to switch to the outer iframe then to inner iframe and then to the element.
Possible solutions-
driver.switchTo().frame("indeedapply-modal-preload-iframe");
driver.switchTo().frame(0);
driver.findElement(By.xpath("//*[#id="form_container"]/div[2]/div[1]/div[1]/p/a")).click();
OR
driver.switchTo().frame(0);
driver.switchTo().frame(0);
driver.findElement(By.xpath("//*[#id="form_container"]/div[2]/div[1]/div[1]/p/a")).click();
I'm trying to made some tests with selenium api on my android device. I've already did the tests in desktop with success, but i'm facing some troubles to make the same program on android.
I created an android project using eclipse IDE, and did the download of selenium here:
http://www.seleniumhq.org/download/ (Java Option)
After, i did the download of the selendroid:
http://selendroid.io/
i've tried to include all these .jar files but when i try to create some AndroidDriver object, my IDE dont recognize this type.
My Class:
package com.example.mobile;
import junit.framework.TestCase;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.mobile.*;
public class OneTeste extends TestCase {
public void testGoogle() throws Exception {
WebDriver driver = new AndroidDriver();
// And now use this to visit Google
driver.get("http://www.google.com");
// Find the text input element by its name
WebElement element = driver.findElement(By.name("q"));
// Enter something to search for
element.sendKeys("Cheese!");
// Now submit the form. WebDriver will find the form for us from the element
element.submit();
// Check the title of the page
System.out.println("Page title is: " + driver.getTitle());
driver.quit();
}
}
package testproject;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class WeblinkTest {
public static void main(String[] args) throws InterruptedException {
// Creating a fierfox driver/window
WebDriver driver= new FirefoxDriver();
//Assigning address of the webpage which you want to check
driver.get("https://www.google.co.in/");
Thread.sleep(2000);
//Creating and Identifing--By.xpath the element on which you want testing
WebElement wb1= driver.findElement(By.xpath(".//*[#id='gb']/div[1]/div[1]/div[1]/div[2]"));
wb1.click();
Thread.sleep(2000);
}
}
Today i was trying to test a gmail link which is available on Google homepage(www.google.co.in). I am able to launch a fierfox window and also it is able to do the first step which is taking me to google homepage but after that nothing is happening neither i am getting any run time error or any exception from eclipse. Don't know what is going on with the webdriver.
I was facing problem with one more program which i already posted on stakwave so if u can then please have a look at this link-Why my test is throwing Exception-Unable to locate element in webdriver?
Please try below. It should work
List<WebElement> elements = driver.findElement(By.LinkText("Gmail"))
elements.get(0).click().
Try with implimentig implicit wait after driver initializes. by adding below line
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
The XPATH in the code is DIV element but you want to click on ANCHOR Gmail So update the xpath for ANCHOR and click on it.
I have tested the below code,it clicks on GMAIL link on google page,if your question is solved then select as answer
WebDriver driver= new FirefoxDriver();
driver.get("https://www.google.co.in/");
WebElement wb1= driver.findElement(By.xpath(".//*[#id='gb']/div[1]/div[1]/div[1]/div[2]/a"));
wb1.click();
This should work,
new WebDriverWait(driver,30).until(ExpectedConditions.visibilityOfElementLocated(By.linkText("Gmail"))).click();
I have posted this question in Jan saying that the web-driver was not able to click on the hyperlink and just now i got the solution.
Actually the xpath for the hyperlink was not accurate.
I had used this xpath-
.//*[#id='gb']/div[1]/div[1]/div[1]/div[2]
which was locating the logo but not the button.
Today i changed it by
.//*[#id='gb']/div[1]/div[1]/div[1]/div[2]/a
and now its working absolutely fine.
Please don't be angry on me because i was going through the questions which i had asked from the forum and found this question. I got the solution for the problem thats why i am sharing this.
I am using Selenium Webdriver(Java), i want to use IE driver for my testing, however i come up with the problem, can anyone help me out of this please, The script which are running fine in firefox fails to run in IE, I am just opening a google page and searching some word but my code only opens the google page write the keyword but unable to hit the serch button on google page using IEdriver, after too much google i found one thimg that when IE browser get opens it will opened in IE8 Compatibility view and due to this its attributes like id, name get changed as compared to FF, but when i changes this to IE8 view manually the properties are same as FF,(Press F12 key on keyboard open developers tool on IE) So can anyone please let me know how to overcome this or how to open the IE browser in IE8 mode, or anyone knows any different solution of using IE for selenium webdriver.
My code is as follows
package backOffice;
import java.io.File;
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.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import com.gargoylesoftware.htmlunit.javascript.configuration.BrowserName;
import bsh.ParseException;
public class Time
{
private WebDriver driver;
private String baseUrl= "http://www.google.co.in/";
public static void main(String args[]) throws InterruptedException
{
Time tm=new Time();
tm.trial();
}
private void trial() throws InterruptedException
{
File file = new File("C:/Documents and Settings/Administrator/Desktop/32- bit_IEDriverServer_Win32_2.31.0/IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
DesiredCapabilities caps = DesiredCapabilities.internetExplorer(); caps.setCapability("ignoreZoomSetting", true);
driver=new InternetExplorerDriver(caps);
driver.get(baseUrl + "/");
driver.findElement(By.id("gbqfq")).clear();
driver.findElement(By.id("gbqfq")).sendKeys("harshal kakade");
driver.findElement(By.id("gbqfb")).click();
driver.findElement(By.linkText("Harshal Kakade - India | LinkedIn")).click ();
}
}
Thanks,
Harshal.
try to look at the internet options -> security and uncheck "Enable Protected Mode". Probably there is the problem.