Selenium ChromeDriver allow cookies ( default profile) - java

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?

Related

How to figure out identifier of input fields from external pop up window

I am trying to put username and password to pop up window. The trouble is that this is some kind of modal or some external window where I can´t find any xpath, id etc... element how to identify this fields. I have came through all topics regarding this but non of them helped me in Java for cucumber/selenium.
Using "https://username:password#wedomain.cz/" doesn´t work.
Using code like this:
WebElement email_id = driver.findElement(By.linkText("Uživatelské
jméno"));
email_id.sendKeys("XXX");
WebElement password_id = driver.findElement(By.linkText("Heslo"));
password_id.sendKeys("xxx");
Thread.sleep(5000);
WebElement sign = driver.findElement(By.linkText("Přihlaste se"));
sign.click();
Doesn´t work. I must also say that domain I go to is different from domain I see on security pop up window.
SOLUTION - need to be done with robot class or another automation library.
https://www.guru99.com/using-robot-api-selenium.html
Closing question.

Checking for URL in JUnit

I'm using the selenium RC driver for JUnit testing my application. Here is the relevant code I am trying to write. It checks that if the user clicks the link, they are taken to the right place.
selenium.click("//td[#id='myLink']/a");
String myURL = selenium.getLocation();
However, when the webdriver simulates a "click," it instead opens up a new window. So now I have the first window open, and the page that I wanted to navigate to is open in a seperate window. Now, when I try to get the URL, selenium grabs the first URL, so it fails the test case. How can I make the page open in the same window? Or, alternatively, how can I get selenium to check the URL of the new window?
EDIT: Should also mention, both my first and second window have the same title, so I don't think I can use selenium.SelectWindow(Title); for this.
Well, with selenium RC being as fickle as it is, I found a work-around for this problem. Here is the brief overview of how I changed my code:
String targetURL = selenium.getAttribute("//td[#id='myLink']/a#href");
selenium.openWindow(targetURL, "myNewWindow");
Thread.sleep(3000); //wait a few seconds for my page to load
selenium.selectWindow("myNewWindow");
selenium.getLocation();
//do my checks
selenium.selectWindow("originalWindowName");
So I retrieve the href myself, open the window using the retrieved URL and with my own custom name. I then switch the context of the driver to the new window and perform my checks. Once I am done, I switch my context back to the original window, and continue with my tests.

How to submit searching field with android keyboard?

I have a problem with submitting searching field with android keyboard in Appium (selenium, java).I didn't find any working solution and stuck at this point. Please, help me.
I tried this:
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("UIATarget.localTarget().frontMostApp().keyboard().buttons()['Done\'].tap()");
But had an error:
org.openqa.selenium.WebDriverException: Not yet implemented. Please help us: http://appium.io/get-involved.html (WARNING: The server did not provide any stacktrace information)
You can't execute JS code that way in appium unless you are in a web view.
For hitting the search key, I would do it like this:
driver.sendKeyEvent(IOSKeyCode.ENTER);
You can send ENTER using:
driver.sendKeyEvent(66); // "66 - KEYCODE_ENTER
This link:
http://developer.android.com/reference/android/view/KeyEvent.html
will give you a list of all the Android KeyEvents, if you click on one of them you can see the actual code for it, like here:
http://developer.android.com/reference/android/view/KeyEvent.html#KEYCODE_ENTER
Hope this helps!
If you are automating mobile web apps, then following is the best way to hide keyboard.
Map<String, Object> BackspaceKeyEvent = new HashMap<>();
BackspaceKeyEvent.put("key", "67");
((JavascriptExecutor)driver).executeScript("mobile:key:event", BackspaceKeyEvent);
This solution in JavaScript worked for me:
Java:
driver.executeScript("mobile: performEditorAction", ImmutableMap.of("action", "search"));
Javascript:
await driver.execute('mobile: performEditorAction', { action: 'search' });
Notice: This special native api just worked for Android.
This allowed me to submit a search command (same as clicking the search icon in the keyboard).
See http://appium.io/docs/en/commands/mobile-command/ for details on "mobile: performEditorAction".

Webdriver hangs in new window with “about:blank” in address bar, transitioning to file download dialog

I need some help for the following issue using Webdriver, Java, and Firefox.
In the testing, when clicking on a link,
1) it will often open a new window with a normal web page. OR
2) occasionally, it will open a new window with “about:blank” in address bar; after 20 to 60 seconds the new window will disappear and a file download window will appear.
Because the URL is rewritten for SEO, there is no way to check the URL before or after clicking on the link to determine whether the link connects to a normal web page or a downloadable file.
In both cases,
driver.getWindowHandles().size() == 2,
so I can switch to the new window successfully using the following statements in order to check whether a certain WebElement exists in new window.
for (String winHandle : driver.getWindowHandles()) {
driver.switchTo().window(winHandle);
}
But if the link connects to a downloadable file, the execution will hang for any of the following methods:
findElement(By.xpath(“//html”));
findElements(By.tagName(“body”));
getCurrentUrl();
getPageSource();
getTitle();
getWindowHandle();
getWindowHandles() always returns 2 while new window with “about:blank” in address bar presents before it is replaced by the file download dialog. It occasionally throws a NoSuchWindowException exception; but most of the time, it just hangs. I tried the Explicit and Implicit Waits to no avail.
Many thanks
Sam
Have you tried setting the following preference? (This will get rid of the download dialog all together)
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.manager.showWhenStarting",false);
FirefoxDriver driver = new FirefoxDriver(profile);

selenium, how can I select new window

I run my selenium rc test in Eclipse with TestNG. I have a link which tries to open a new browser page. How can I select this new page to operate in? I use this code:
selenium.selectWindow("name=NewPage");
however it says page not found. I also try to define page ids or titles with this code:
String[] wins = selenium.getAllWindowIds();
for (String s : wins)
System.out.println("win: " + s);
It does not define my new opened window:
win: MainPage
win:
If use selenium.getAllWindowNames() I get win: selenium_main_app_window
win: selenium_blank65815.
I write this code selenium.selectWindow("name=blank99157"); but get the error - ERROR: Window does not exist. If this looks like a Selenium bug, make sure to read http://seleniumhq.org/docs/02_selenium_ide.html#alerts-popups-and-multiple-windows for potential workarounds.
The window obviously has no name, so you can't select it by name.
If the window is opened via JavaScript and you can change the script, try changing window.open("someUrl"); to window.open("someUrl", "someName");, you'll be then able to select the window by the set name. More information on the MDN doc for window.open().
Selenium RC doesn't support <a href="someUrl" target="_blank"> links (which open the link in a new window). Therefore, if the window is opened by a link of this type, you have to find this <a> element, get the href attribute and call
selenium.openWindow(theFoundUrl, "theNewWindow");
selenium.selectWindow("id=theNewWindow");
If it is opened via JavaScript before or during the onload event, you'll need to call
selenium.openWindow("", "theNewWindow");
selenium.selectWindow("id=theNewWindow");
More information on this in the bug SEL-339 or in the openWindow() and selectWindow() JavaDocs.
If you have only two windows / want to open the newest one, you can try
selenium.selectPopup()
That is, obviously, the easiest way, because it selects the first non-top window. Therefore, it's only useful when you want to select the newest popup.
If the new window has a unique title, you can do
selenium.selectPopup("Title of the window");
or selenium.selectWindow("title=Title of the window");
Otherwise, you must iterate over selenium.getAllWindowNames() to get the right name (Selenium creates names for windows without one). However, you can't hardcode that name into your testcase, because it will change every time, so you'll need to work out some dynamic logic for this.
You won't like this: Go for WebDriver. It should be far more resistant to such problems.
WebDriver driver = new FirefoxDriver();
WebElement inputhandler = driver.findelement(By.linktext("whatever here"));
inputhandler.click();
String parentHandle = driver.getWindowHandle();
Set<String> PopHandle = driver.getWindowHandles();
Iterator<String> it = PopHandle.iterator();
String ChildHandle = "";
while(it.hasNext())
{
if (it.next() != parentHandle)
{
ChildHandle = it.next().toString();
// because the new window will be the last one opened
}
}
driver.switchTo().window(ChildHandle);
WebDriverWait wait1 = new WebDriverWait(driver,30);
wait1.until(ExpectedConditions.visibilityOfElementLocated(By.id("something on page")));
// do whatever you want to do in the page here
driver.close();
driver.switchTo().window(parentHandle);
You might not be using the correct window ID.
Check out this link. You might find your answer here.
Let me know you this helps.
Try selenium.getAllWindowNames(), selenium.getAllWindowTitles()..one of them will work for sure.

Categories

Resources