Running Selenium with Chrome driver and disable chrome browser view - java

I'm using the following driver :
WebDriver m_driver = new ChromeDriver();
WebDriverWait waitableDriver = new WebDriverWait(m_driver , 10);
The first action is:
waitableDriver.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[#id='"+selectionID+"']"))).click();
this is will sellect an id on the page, and then i'm starting to grab the information. the problem is that while using
WebDriver m_driver = new ChromeDriver();
everything works just fine. while trying to change it to :
m_driver = new HtmlUnitDriver();
the above click action is not working, is there any way to fix it / using chrome driver and disable the view of the browser ?

Related

Selenium can't open Gitlab login page

I'm doing an automated test, using Java (8), Selenium (4) and Chromedriver (98.0).
The test is for a site that requires to login with different third party accounts, one of them being GitLab.Unfortunately the test always gets stuck while trying to access Gitlab's login page, on the "Checking your browser before accessing gitlab.com" part. If I pause the test on this step and duplicate the tab manually, the newly opened tab will be able to enter and then the first one will be also able to do it (probably because at that point finally has a valid cookie).I've tried out different solutions but with no luck. Currently this is my code:
#Test
public void test() throws MalformedURLException {
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-dev-shm-usage");
options.addArguments("--disable-blink-features=AutomationControlled");
options.addArguments("--start-maximized");
options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
options.setExperimentalOption("useAutomationExtension", false);
RemoteWebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), options);
driver.navigate().to("https://gitlab.com/users/sign_in");
new FluentWait<>(driver)
.withTimeout(Duration.ofSeconds(60))
.pollingEvery(Duration.ofSeconds(2))
.until(x -> driver.findElements(By.xpath("//*[#data-translate='checking_browser']")).size() == 0);
}
When trying to use undetected_chromedriver, using Python, it worked, but it's a requirement for me to use Java. Is there something similar for Java or is there an extra ChromeOption that I'm missing?
need to download chrome driver https://chromedriver.chromium.org/downloadshttps://chromedriver.chromium.org/downloads
and add this code while setting up driver
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
WebDriver driver = new ChromeDriver();

Selenium - Log in and navigate to different page

I want to log into facebook, and then open a specific facebook url to get a list of people, working for a specific company (e.g. to get the employees of google, I need to go to https://www.facebook.com/search/str/google/pages-named/employees/present/intersect
) My first thought was to simply login (which works fine), and then go to the specific page using driver.navigate().to()
FirefoxDriver driver = new FirefoxDriver();
driver.get("https://www.facebook.com");
WebElement emailElement = driver.findElementById("email");
WebElement passwordElement = driver.findElementById("pass");
emailElement.sendKeys("xxxx#xxxx.com");
passwordElement.sendKeys("xxxx");
emailElement.submit();
driver.navigate().to("https://www.facebook.com/search/str/google/pages-named/employees/present/intersect");
However, this way, the facebook page is not available, and I’m prompted to log in again, even though the page is opened in the same browser tab?!
The second thought was to log in first, get the cookie, and then use is this cookie for a new driver:
FirefoxDriver driver = new FirefoxDriver();
driver.get("https://www.facebook.com");
WebElement emailElement = driver.findElementById("email");
WebElement passwordElement = driver.findElementById("pass");
emailElement.sendKeys("xxxx#xxxx.com");
passwordElement.sendKeys("xxxxx");
emailElement.submit();
Set<Cookie> cookies = driver.manage().getCookies();
FirefoxDriver driver2 = new FirefoxDriver();
for (Cookie cookie : cookies) {
Cookie cookieNew = new Cookie.Builder(cookie.getName(), cookie.getValue()).expiresOn(cookie.getExpiry())
.isHttpOnly(cookie.isHttpOnly()).isSecure(cookie.isSecure()).path(cookie.getPath()).build();
driver2.manage().addCookie(cookieNew);
}
driver2.get("https://www.facebook.com/search/str/google/pages-named/employees/present/intersect");
}
However, that way, an exception is thrown: org.openqa.selenium.InvalidCookieDomainException: Document is cookie-averse
What am I doing wrong?
I don't think it is a mandatory to store cookies to to get a list of people, working for a specific company. The following code block works well at my side :
driver.findElement(By.cssSelector("input[value='Log In']")).click();
driver.get("https://www.facebook.com/search/str/google/pages-named/employees/present/intersect");
System.out.println(driver.getTitle());
driver.quit();
Console Output :
Facebook Search
Apply First Thought: Setting up geckodriver for Selenium Java resolves the issue I think.
It needs to set geckodriver path with FirefoxDriver as below code:
System.setProperty("webdriver.gecko.driver", "PATH/TO/geckodriver.exe");
FirefoxDriver driver = new FirefoxDriver();
Download geckodriver for your suitable OS from https://github.com/mozilla/geckodriver/releases
Extract it in a folder of your choice
Set the path correctly as mentioned above
After submit() you need to use wait functionality to refresh the page like
emailElement.submit();
//Use WebDriver wait.until() or sleep() to finish the submit action
driver.navigate().to("https://www.facebook.com/search/str/google/pages-named/employees/present/intersect");

Firefox Error: "Your connection is not secure" while launching driver with Selenium 3.0.1 using Java

My Firefox version is 46.0.1 and Selenium version is 3.0.1.
I am getting error:
Your connection is not secure
while executing following code:
#Test
public void test() {
ProfilesIni profile = new ProfilesIni();
FirefoxProfile ffProfile = profile.getProfile("newCretedProfile");
ffProfile.setAcceptUntrustedCertificates(true);
ffProfile.setAssumeUntrustedCertificateIssuer(false);
System.setProperty("webdriver.gecko.driver", "D:\\SELENUIUM\\Drivers\\geckodriver.exe");
FirefoxDriver driver = new FirefoxDriver(ffProfile);
driver.get("http://www.google.com");
driver.quit();
}
I have created new firefox profile and followed steps from this url
Nevertheless it's not working and giving me same error while I launching any site.
Download Firefox 55 beta and set
capabilities.setCapability("acceptInsecureCerts", true);
Here is my code that works for Firefox 55 beta:
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setBrowserName("firefox");
capabilities.setCapability("acceptInsecureCerts", true);
RemoteWebDriver driver = new RemoteWebDriver(Environment.remoteWebDriverURL, capabilities);
I have tried this approach and it worked well for me.
Create new firefox profile by following below step.
Close all your firefox windows
In the Run dialog box, type in: ‘firefox.exe -p' and then Click OK.
Click “Create Profile”
Create a name for your new profile(say Selenium)
Click “Choose Folder”
Pick location something easy to find — like “C:\NewFirefoxProfile”
Click Finish
Now after selecting newly created profile, start Firefox. Open the specific url you were getting 'Secure Connection Issue', accept SSL certificates for this profile.
Now use the newly created firefox profile to run your selenium test. Modify below code as per your requirement.
System.setProperty("webdriver.firefox.marionette","D:\\SELENUIUM\\Drivers\\geckodriver.exe");
ProfilesIni profile = new ProfilesIni();
FirefoxProfile myprofile = profile.getProfile("C:\\NewFirefoxProfile");//location of your new firefox profile
WebDriver driver = new FirefoxDriver(myprofile);
driver.get("https://cacert.org/");
With FF v53+ and Se 3.x (summer 2017), advice from before (May?) 2017 is no longer true.
You have to use Marionette and set capability to True.
Took me few days to sort out all old and obsolete advice, yours for free. :-)
Looks like it is not supported yet by geckodriver/Marionette.
You can check below bugs for more information:-
https://github.com/mozilla/geckodriver/issues/93
https://bugzilla.mozilla.org/show_bug.cgi?id=1103196
If you want to run the tests on Firefox with Selenium 3.0 set the Firefox driver capability “marionette” to false.
#Test
public void test() {
DesiredCapabilities d = new DesiredCapabilities();
d.setCapability("marionette", false);
WebDriver driver = new FirefoxDriver(d);
driver.get("http://www.google.com");
driver.quit();
}

selenium get() doesn't do anything

driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
The code below should open a firefox browser, navigate to google and search for "automation" and should navigate again to yahoo. But driver.get("http://www.yahoo.com") doesn't do anything. How will I change URL using selenium?
driver.get("http://www.google.com");
widget=driver.findElement(By.id("lst-ib"));
widget.click();
widget.sendKeys("automation");
widget.sendKeys(Keys.ENTER);
driver.get("http://www.yahoo.com");
widget=driver.findElement(By.xpath(".//*[#id='yui_3_12_0_1_1452245228407_940']/td[1]/a"));
widget.click();
The same is working fine for me after pausing/stopping the my kaspersky internet security.
driver.get("http://www.google.com");
driver.findElement(By.id("lst-ib")).sendKeys("seleniumhq");
driver.findElement(By.id("lst-ib")).sendKeys(Keys.ENTER);
Thread.sleep(6000);
driver.get("http://www.yahoo.com");
Assuming you are running code on Windows, can you check your hosts file entry. You can find it in below location:
C:\Windows\System32\Drivers\etc\hosts
Have a look at the file content and check if localhost resolution has been altered.
If it does not help, post selenium version you are using, error message which you are getting.
You cannot open in same object. Two Options
Option 01:
You can open in new window like this
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
WebElement widget = driver.findElement(By.id("lst-ib"));
widget.click();
widget.sendKeys("automation");
widget.sendKeys(Keys.ENTER);
WebDriver seconddriver = new FirefoxDriver();
seconddriver.get("http://www.yahoo.com");
Option 02:
you can use keyboard keys to open new window or tab and then using driver.switchto.

Selenium 2: New Browser Has No Bookmarks

When I use Selenium 2 code (Java) to open Firefox (or any other browser) for some automated tests, the new window opens without my bookmarks, or for that matter the bookmark bar. Additionally, I suspect that cookies aren't retrieved either, because sites I normally log into do not remember certain things from my previous history.
The relevant code:
//WebDriver driver = new FirefoxDriver();
WebDriver driver = new InternetExplorerDriver();
String baseUrl = "http://localhost:8080/";
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//Navigate to login page
driver.navigate().to(baseUrl + "/myApp");
//obtain the username and password elements
WebElement username = driver.findElement(By.name("username"));
WebElement password = driver.findElement(By.name("password"));
//log in
username.sendKeys("myTestLogin");
password.sendKeys("myTestPwd");
driver.findElement(By.cssSelector("input.btnStyle")).click();
...
I think by default Selenium (WebDriver) will try to use as "clean" of a profile as possible. This is so the browser's settings that a user set up don't cause testing failures. You can modify these settings if you need to. Check out http://code.google.com/p/selenium/wiki/TipsAndTricks and see if that helps get you on the right track. I haven't done this with IE before though. I think with Firefox you can even have Selenium use an existing profile if you really need it to.

Categories

Resources