how to avoid google search detect selenium webdriver as unusual behaviour? - java

I try to use selenium webdriver to do one search by image in google so my user didn't need to manually open the browser and paste image url there. but google say
Our systems have detected unusual traffic from your computer network. This page checks to see if it's really you sending the requests, and not a robot.
And give captcha, is there a way to avoid being detected as automation by google using selenium webdriver?
here my code:
#Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "http://images.google.com/searchbyimage?image_url=";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
#Test
public void test2() throws Exception {
driver.get(baseUrl + "http://somesite.com/somepicture.jpg");
driver.findElement(By.linkText("sometext"));
System.out.println("finish");
}
#After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}

It appear that google detect browser profile to determine something strange has going on or not. for example if you do private browsing with your browser(i test it with firefox and chrome), your browser profile will change to anonymous, so google will find it suspicious and request you to fill captcha.
That case also happen when you run your browser from selenium webdriver.
So you need to set the selenium driver profile to your default profile by using some code like this(currently only work on firefox)
ProfilesIni allProfiles = new ProfilesIni();
WebDriver driver = new FirefoxDriver(allProfiles.getProfile("default"));

I disagree with #Angga and doubt that Google knows that you're a bot because you're NOT in your default profile.
It's more likely because of this:
Can a website detect when you are using selenium with chromedriver?

Just add a single line, it will surely help
#For ChromeDriver version 79.0.3945.16 or over
options.add_argument('--disable-blink-features=AutomationControlled')
#Open Browser
browser = webdriver.Chrome(executable_path='chromedriver.exe',options=option)

Related

I am getting this error"Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code"

I have tried so many times.
It launches a browser but doesn't perform any other tasks.
I am using Java 1.8 , chrome Version 80.0.3987.149 (Official Build) (64-bit), chrome driver version 80.0.3987.149.
I don't know what's wrong I am doing.
enter image description here
The best way to create chrome driver is by using chrome service with allocating free ports.
private static ChromeDriverService service;
private WebDriver driver;
#BeforeClass
public static void createAndStartService() throws IOException {
service = new ChromeDriverService.Builder().usingDriverExecutable(new File("/path/to/chromedriver")).usingAnyFreePort().build();
service.start();
driver = new RemoteWebDriver(service.getUrl(), new ChromeOptions());
}

Clear InternetExplorerDriver cookies selenium webdriver

When I create my internetExplorer instance, I use the following:
public static WebDriver internetExplorerWebWDriver() {
DesiredCapabilities returnCapabilities = DesiredCapabilities.internetExplorer();
returnCapabilities.setCapability(InternetExplorerDriver.ENABLE_PERSISTENT_HOVERING, false);
returnCapabilities.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true);
returnCapabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
returnCapabilities.setCapability("ignoreZoomSetting", true);
return new InternetExplorerDriver(returnCapabilities);
My problem is: I have to open a secondary internetExplorer window with cleared cookie/Cache/Session and authenticate a user during the login.
Right now, using this code, cookie is not deleted because authentication not appears and I cannot login with different user. (seems to me, the first login is saved, and used in the second window)
Any ideas? Thanks!
Have you tried restarting IE after calling DeleteAllCookies each time ?
Placing a driver.quit() in the after class if you using junit ?
I have experimented similar problems, try with the code below in #Before method:
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("localStorage.clear();");
js.executeScript("sessionStorage.clear();");
driver.manage().deleteAllCookies();

Fullscreen operation is not working in selenium webdriver 3.x

Firefox Version: 52.0.2 (32-bit)
Platform: Windows 7
Selenium Webdriver version: 3.4.0 (Java bindings)
Problem statement:
While trying to perform full screen operation in firefox browser then it throws UnsupportedCommandException
Test Code:
public class GeckoTest {
public static void main(String[] args) throws IOException {
System.setProperty("webdriver.gecko.driver","<geckodriver executable>");
FirefoxBinary binary = new FirefoxBinary(new File("firefox binary"));
FirefoxOptions options = new FirefoxOptions();
options.setBinary(binary);
options.setLogLevel(Level.ALL);
WebDriver browser = new FirefoxDriver(options);
browser.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
browser.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
browser.get("http://examples.sencha.com/extjs/6.5.0/examples/kitchensink/?classic#form-fieldtypes");
browser.manage().window().fullscreen();
WebDriverWait wait = new WebDriverWait(browser,20,3000);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//div[contains(#class,'x-form-spinner x-form-spinner-default x-form-spinner-down x-form-spinner-down-default')]")));
Actions builder = new Actions(browser);
builder.doubleClick(browser.findElement(By.xpath(".//div[contains(#class,'x-form-spinner x-form-spinner-default x-form-spinner-down x-form-spinner-down-default')]"))).perform();
browser.close();
}
}
EDIT: It seems this is an known issue and will be fixed in FF55 as per this enter link description here
While you work with Selenium 3.4.x, geckodriver v0.16.1 and Mozilla Firefox 53.0, as you mentioned when we try to perform full screen operation in Mozilla Firefox browser then it throws UnsupportedCommandException is true. How ever there is an alternative to achieve the full screen operation in Mozilla Firefox which works perfect through sending F11 Keys. Here is the minimal code block to check full screen operation in Mozilla Firefox:
System.setProperty("webdriver.gecko.driver", "C:\\your_directory\\geckodriver.exe");
WebDriver browser = new FirefoxDriver();
browser.get("http://examples.sencha.com/extjs/6.5.0/examples/kitchensink/?classic#form-fieldtypes");
browser.findElement(By.tagName("body")).sendKeys(Keys.F11);

How to open a mobile version website using Selenium WebDriver?

I have the following code to open the mobile version of Facebook in Firefox in my Desktop by changing the user-agent.
#Test
public void fb() {
FirefoxProfile ffprofile = new FirefoxProfile();
ffprofile.setPreference("general.useragent.override", "iPhone"); //this will change the user agent which will open mobile browser
WebDriver driver = new FirefoxDriver(ffprofile);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().setSize(new Dimension(400,800)); //just to change the window size so that it will look like mobile ;)
driver.navigate().to("http://www.facebook.com/");
driver.findElement(By.name("email")).sendKeys("username");
driver.findElement(By.name("pass")).sendKeys("************");
driver.findElement(By.name("login")).click();
}
But for some reason, it doesn't seem to work quite right. Can anyone let me know what I'm doing wrong here?
try sending a request to http://m.facebook.com, this is facebooks mobile website, This will only work if its just for facebook though.

Webdriver - HTTP authentication dialog

I have a very simple selenium-webdriver script. I would like to do HTTP authentication using webdriver.
Script:
WebDriver driver = new FirefoxDriver();
driver.get("http://www.httpwatch.com/httpgallery/authentication/");
driver.findElement(By.id("displayImage")).click();
Thread.sleep(2000);
driver.switchTo().alert().sendKeys("httpwatch");
Issue:
driver.switchTo().alert().sendKeys("httpwatch");
throws
org.openqa.selenium.NoAlertPresentException: No alert is present
Question:
Does Webdriver find only an alert dialog as alert?
What are my options to automate this without using AutoIt OR http:// username:password #somesite
EDIT
Alert has below method and does not seem to have been implemented yet.
driver.switchTo().alert().authenticateUsing(new UsernameAndPassword("username","password"))
The problem is that this is not a javascript popup hence you cannot manipulate it via selenium's alert().
If both AutoIt and submitting credentials in the URL (the easiest option - just open up the url and click "Display Image") are not options for you, another approach could be to use AutoAuth firefox addon to automatically submit the previously saved credentials:
AutoAuth automatically submits HTTP authentication dialogs when you’ve
chosen to have the browser save your login information. (If you’ve
already told the browser what your username and password are, and
you’ve told it to remember that username and password, why not just
have it automatically submit it instead of asking you each time?)
Following the answer suggested in HTTP Basic Auth via URL in Firefox does not work? thread:
Install AutoAuth Firefox plugin;
Visit the site where the authentication is needed. Enter your username and password and make sure to choose to save the credentials;
Save AutoAuth installation file at your hard drive: at the plugin page, right click at “Add to Firefox” and “Save link as”;
Instantiate Firefox webdriver as following:
FirefoxProfile firefoxProfile = new ProfilesIni().getProfile("default");
File pluginAutoAuth = new File("src/test/resources/autoauth-2.1-fx+fn.xpi");
firefoxProfile.addExtension(pluginAutoAuth);
driver = new FirefoxDriver(firefoxProfile);
Also, in a way similar to AutoIt option - you can use sikuli screen recognition and automation tool to submit the credentials in the popup.
Also see other ideas and options:
Support BASIC and Digest HTTP authentication
Handling browser level authentication using Selenium
The Basic/NTLM authentication pop-up is a browser dialog window. WebDriver (Selenium 2.0) cannot interact with such dialog windows. The reason for this is because WebDriver aims solely at mimicking user interaction with websites, and browser dialog windows are currently not in that scope. JavaScript dialog windows, are part of the website, so WebDriver can handle those. In Selenium 1.0 it is possible to do basic authentication.
So how to solve this issue? 1) Authentication via URL http://username:password#website.com 2) Use a browser plugin that will handle the Basic/NTLM autentication 3) Use a local proxy that will modify the request header and pass along the username/password and 4) Make use of a robot, like AutoIt, or some Java library.
Option 1: is the easiest and has the least impact on the system/test. Option 2: has a high browser impact as your loading plugins. Also every browser uses its own plugin and it's possible that the required plugin for a certain browser is not available. Option 3: Works well with HTTP, but HTTPS requires custom certicates thus not always an option. Not much impact on both system and test. Option 4: Mimics keyboard presses, its a go to solution but prone to errors. Only works when the dialog window has focus and it is possible that this is not always the case.
I faced same issue, and got some concrete solution using robot class. Its workaround or solution, Let see , but it works.
public class DialogWindow implements Runnable {
#Override
public void run() {
try {
entercredentials();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void entercredentials() InterruptedException {
Thread.sleep(5000);
try {
enterText("username");
enterSpecialChar(KeyEvent.VK_TAB);
enterText("password");
enterSpecialChar(KeyEvent.VK_ENTER);
} catch (AWTException e) {
}
}
private void enterText(String text) throws AWTException {
Robot robot = new Robot();
byte[] bytes = text.getBytes();
for (byte b : bytes) {
int bytecode = b;
// keycode only handles [A-Z] (which is ASCII decimal [65-90])
if (bytecode> 96 && bytecode< 123)
bytecode = bytecode - 32;
robot.delay(40);
robot.keyPress(bytecode);
robot.keyRelease(bytecode);
}
}
private void enterSpecialChar(int s) throws AWTException {
Robot robot = new Robot();
robot.delay(40);
robot.keyPress(s);
robot.keyRelease(s);
}
}
How to call it
WebDriver driver= new FirefoxDriver()// or any other driver with capabilities and other required stuff
(new Thread(new DialogWindow())).start();
driver.get(url);

Categories