how to enable cookies/javascript - java

I'm trying to create a scenario on chegg.com
but it seems that it is programmed to detect automation tools
enter image description here
is there a way to enable javascript and cookies before and preconfig or at least a way to not trigger this issue?

var options = new ChromeOptions();
options.AddArguments("--enable-javascript");
options.AddUserProfilePreference("profile.cookie_controls_mode", 0);
driver = new ChromeDriver("C:\\Program Files (x86)\\Google\\Chrome\\Application", options);
driver.Navigate().GoToUrl(loginUrl);
Try the above for javascript and cookies enabled. The code is in c#. You may try the desired option in your own programming language.

Related

Selenium ChromeDriver allow cookies ( default profile)

I did a search and I couldn't find this type of problem posted on stack overflow.
And I did read documentation on the https://chromedriver.chromium.org/capabilities but couldn’t find any meaningful examples. I found examples about add cookies but not about allow a cookie.
The problem:
Basically, I try to access and scrape a site but as soon as I try to access the url with:
webDriver.get(urlAddress);
a pop-up window appears which prompt me to accept all cookies.
My work around now is to click the button like in the code below.
List<WebElement> acceptAllCookies =
webDriver.findElements(By.id("ccc-notify-accept"));
if (!acceptAllCookies.isEmpty()) {
acceptAllCookies.get(0).click();
WebDriverWait wait = new WebDriverWait(webDriver, 2);
}
And then proceed with scraping.
I went to chrome://settings and add the site on to the “Sites that can always use cookies” and the pop up doesn’t show when I access it manually. But when I try programmatically the pop-up appears again.
I do not know how to load my chrome profile . I tried
ChromeOptions options = new ChromeOptions();
//Path to your chrome profile which allows
//cookyes from londonstockexchange
options.addArguments("user-data-dir= My User Data\\Default");
driver = new ChromeDriver(options);
But didn’t seem to work. I do not know how to configure the capabilities to allow cookies only for that specific site
Will be something like:
options.setCapability("____", _____);
I don't know what to add in the blanks to always allow cookie for a specific site.
Can someone please help fill in the blanks above or point me to some examples?

How can I set my Firefox browser in private mode in Selenium 2.53.1?

I'm on selenium 2.53.0 (I don't want to update to 3.0 because I need to work on Firefox and Selenium did not implement actions for FF).
My problem is: I try to open FF in private mode (I don't want to keep the cache because I ran a lot of FF instances)
For that, I use a FF profile and I have a certificate, so I accept it.
My code is :
FirefoxDriverManager.getInstance().setup();
capabilities.setBrowserName("firefox");
capabilities.setVersion("46");
ProfilesIni profile = new ProfilesIni();
FirefoxProfile ffProfile = profile.getProfile("default");
ffProfile.setPreference("browser.private.browsing.autostart", true);
ffProfile.setPreference("browser.privatebrowsing.autostart", true);
//accept the certificate
ffProfile.setAcceptUntrustedCertificates(true);
ffProfile.setAssumeUntrustedCertificateIssuer(false);
capabilities.setCapability(FirefoxDriver.PROFILE, ffProfile);
WebDriver webDriver = new FirefoxDriver();
I tried "browser.private.browsing.autostart" and "browser.privatebrowsing.autostart" because when I did about:config in firefox, i found this two.
I did not received any error, firefox run my test but not in private. Do you have any idea?
I found this post but it haven't have answer.
FirefoxOptions opts = new FirefoxOptions();
opts.addArguments("-private");
FirefoxDrive f = new FirefoxDriver(opts);
working currently with FF v54 and selenium 3.4.0
On your code sample above you are trying twice to set the profile. Try removing one of the arguments? AS long as
ffProfile.setPreference("browser.privatebrowsing.autostart", true);
is not working for you, there is an alternative workaround solution; so you can work until you figure this out.
Find any element at the page you are visiting where you want the private window to be opened. For example:
Driver.FindElement(By.ByXpath(//div[#id="loginButton"])).sendKeys(Keys.chord(Keys.CONTROL, Keys.SHIFT, "P"));
And then send a Ctrl+Shift+p to it..Then you can use the new private window for your test. Perhaps not the best solution but, this will work 100%, just tested on my machine, please comment below if you have trouble getting this right.
Best of luck!

What kind of window is this and how do I handle it with Selenium? Autoit?

I have a dialog that Windows Detective claims is a "MozillaDialogClass" window, I'm testing in Firefox right now of course.
Here is an image of the window.
AutoIt IDE doesn't recognize it. Windows Defender can't see any of it's properties so I have no idea what the name of it is or anything else about it. One of the developers says it's a browser specific window. (whatever the heck that is, if it is why can't anything see it?).
Anyone have any ideas on this one? I'm stumped.
The best way to handle this is to use SikuliX. Below is an example scenario using SikuliX. Basically, SikuliX automates anything you see on the screen of your desktop computer running Windows, Mac or some Linux/Unix. It uses image recognition powered by OpenCV to identify and control GUI components. This is handy in cases when there is no easy access to a GUI's internals or the source code of the application or web page you want to act on.
Below is a hello world example. This clicks on the spotlight icon on the screen, waits spotlight’s input window appears, and then types “hello world” and hits ENTER.
import org.sikuli.script.*;
public class TestSikuli {
public static void main(String[] args) {
Screen s = new Screen();
try{
s.click("imgs/spotlight.png", 0);
s.wait("imgs/spotlight-input.png");
s.type(null, "hello world\n", 0);
}
catch(FindFailed e){
e.printStackTrace();
}
}
}
You can find sikuli here
This is a native Dialog from your webbrowser. So it is not easy to handle this with Selenium. What do you want to test in it?
For some reading, see also this question:
https://sqa.stackexchange.com/questions/2197/how-to-download-a-file-using-seleniums-webdriver
Rather than handling the download window, you could setup Firefox to automatically download the file to a folder:
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("pdfjs.disabled", true); // disable the built-in viewer
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.download.dir", "C:\\Windows\\temp");
profile.setPreference("browser.download.panel.shown", false);
profile.setPreference("browser.helperApps.neverAsk.openFile", "");
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/vnd.ms-excel");
WebDriver driver = new FirefoxDriver(profile);
driver.get("http://www.exinfm.com/free_spreadsheets.html");
driver.findElement(By.linkText("Capital Budgeting Analysis")).click();
Or to automatically open a file with the default application:
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("pdfjs.disabled", true); // disable the built-in viewer
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.download.panel.shown", false);
profile.setPreference("browser.helperApps.neverAsk.openFile", "application/vnd.ms-excel");
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "");
WebDriver driver = new FirefoxDriver(profile);
driver.get("http://www.exinfm.com/free_spreadsheets.html");
driver.findElement(By.linkText("Capital Budgeting Analysis")).click();
Note that you need to update the MIME type (application/vnd.ms-excel here) by the one of your file.
The MIME type is the Content-Type from the response header.
And if you just want to close the dialog, then I would use AutoIt:
WinWait("[CLASS:MozillaDialogClass]", "", 10)
WinClose("[CLASS:MozillaDialogClass]")

Disable native events on chrome?

Hi I want to disable native events from Chrome driver, I did not find the way to do it, I just found examples for Firefox:
dcBrowser = DesiredCapabilities.firefox();
FirefoxProfile prof2 = new FirefoxProfile();
prof2.setEnableNativeEvents(false);
dcBrowser.setCapability("firefox_profile", prof2);
I tried to do the same for Chrome
dcBrowser = DesiredCapabilities.chrome();
ChromeProfile prof2 = new ChromeProfile();
prof2.setEnableNativeEvents(false);
dcBrowser.setCapability("chrome_profile", prof2);
When I add the import:
import org.openqa.selenium.chrome.ChromeProfile;
I get the following error:
The import org.openqa.selenium.chrome.ChromeProfile cannot be resolved
I know the ChromeProfile class does exists, but since I didn't find any example of disabling chrome native events I don't know if what I did is correct or not or maybe I am missing something, any ideas? Thanks!
There is no equivalent. Chrome always has native events enabled.
See: https://github.com/SeleniumHQ/selenium/wiki/Advanced-User-Interactions#native-events-versus-synthetic-events
If you are using java, you can simply use
super.setNativeEvents(false);

Enabling Do Not Track

Is there a way to enable the "Do not track" option for selenium using chromedriver?
None of the command line switches seem to help and this website says that the option is disabled when run with chromedriver even though my regular Chrome profile has it turned on. I want to use a temporary profile and not load my existing one though.
Suggestions? Can the option be set automatically?
I figured it out. It can be done by setting the preferences from the Chrome preferences file like this:
Map<String, Object> preferences = new HashMap<String, Object>();
ChromeOptions options = new ChromeOptions();
preferences.put("enable_do_not_track", true);
options.setExperimentalOption("prefs", preferences);
For those working with the Python, we were able to follow the Java answer above and do the following:
options = webdriver.ChromeOptions()
prefs = {"enable_do_not_track": True}
options.add_experimental_option("prefs", prefs)
#pynterest's answer above works in Python. For those of you who wish to confirm this try:
options = webdriver.ChromeOptions()
no_track = {"enable_do_not_track": True}
options.add_experimental_option("prefs", no_track)
b = webdriver.Chrome(options=options)
b.get('https://www.whatismybrowser.com/detect/is-do-not-track-enabled')
Which should open up a selenium browser telling you that Do Not Track is enabled.

Categories

Resources