selenium get() doesn't do anything - java

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.

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

Login failed message does not appear on a website while using selenium

I am trying to log in into a website using selenium.The end site provides a access denied message if i enter wrong credentials and submit the login form(without selenium).But when i do the same stuff using selenium,the access denied message does not appear. Any suggestions?
URL : https://oa.lombardfinance.com.au/
ChromeOptions options = new ChromeOptions();
options.addArguments("disable-infobars");
WebDriver driver = new ChromeDriver(options);
JavascriptExecutor js = (JavascriptExecutor)driver;
driver.get("https://oa.lombardfinance.com.au/");
driver.findElement(By.id("ctl00_ContentPlaceHolder1_txtClientNumber")).sendKeys("Subrat");
driver.findElement(By.id("ctl00_ContentPlaceHolder1_txtPassword")).sendKeys("Subrat");
driver.findElement(By.id("ctl00_ContentPlaceHolder1_txtCaptcha")).sendKeys("Subrat");
//WebElement loginForm = driver.findElement(By.id("aspnetForm"));
//js.executeScript("arguments[0].submit();", loginForm);
WebElement login = driver.findElement(By.id("ctl00_ContentPlaceHolder1_btnLogin2"));
new Actions(driver).moveToElement(login).click().perform();
Just tried the same code you're using with a slight difference in driver initiation:
System.setProperty("webdriver.chrome.driver", "\path\to\chromedriver.exe");
WebDriver driver = new ChromeDriver();
I excluded the line:
options.addArguments("disable-infobars");
and the Selenium test shows Access denied as well.
Try using the below code:
System.setProperty("webdriver.chrome.driver", "\\path\\to\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("--test-type");
options.addArguments("--disable-extensions");
WebDriver driver = new ChromeDriver(options);
The solution is more convenient one because some times browser shows developer mode issue.

Running Selenium with Chrome driver and disable chrome browser view

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 ?

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