I am currently working in a selenium with java automation proyect.
The web page I am automating opens a side menu depending on the size of the screen. In my case, it does not open it. To solve this you can either clic on the menu button to open it or change the zoom.
I am trying to implement the second solution zooming (this is the solution I need):
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("document.body.style.zoom='70%'");
The zoom works but the side menu does not appear. Is there anything extra that I need to do?
I also use the next line as part of my configurations:
ChromeOptions options = new ChromeOptions();
options.addArguments("window-size=1980,1080");
I also tried different ways to zoom in but the results are the same:
driver.findElement(By.tagName("html")).sendKeys(Keys.CONTROL,Keys.SUBTRACT);
WebElement html = driver.findElement(By.tagName("html"));
new Actions(driver)
.sendKeys(html, Keys.CONTROL, Keys.SUBTRACT, Keys.NULL)
.perform();
Any suggestions? I would appreciate them because I am new to selenium and I am pretty stuck with this issue.
driver = new ChromeDriver();
JavascriptExecutor jse = (JavascriptExecutor)driver;
driver.get("chrome://settings/");
jse.executeScript("chrome.settingsPrivate.setDefaultZoom(0.9);");
driver.get("...");
This is how I managed myself to do zoom correctly.
Related
I have a element with data-qa-id = "journal-submit";
When I ran the test with small screen resolution Selenium says this element is not visible and cannot perform any action on it.
The same element is visible to selenium in regular chrome resolution. Can anyone explain me why it is so ? How to handle it ?
There's a few options for solutions here. I think scrolling to an element can be a bit hacky, because then it will remove other elements from the view of the browser, so you'll end up having to scroll all over the place to locate elements that may or may not be in view of the browser.
But, if you wish to use a scrolling solution, you can try something like this:
// declare JS executor
JavascriptExecutor executor = (JavascriptExecutor)driver;
// this is the element you want to find
WebElement element = driver.findElement(someLocatorHere);
// scroll to the element
executor.executeScript("arguments[0].scrollIntoView(true);", element);
I'm not a huge fan of this solution, for reasons I mentioned above. A "true" solution that will solve all problems of this type is to run your browser in headless and specify the screen size in the driver settings.
// declaring the webdriver
// headless options
ChromeOptions headlessOptions = new ChromeOptions();
headlessOptions.AddArgument("--headless");
headlessOptions.AddArguments("--disable-gpu");
headlessOptions.AddArguments("--window-size=1920,1200");
// start driver
driver = new ChromeDriver(headlessOptions);
I prefer the headless solution because it will solve most, if not all, of your issues regarding smaller screen resolution. I've gone back and forth with screen resolution issues for the majority of my career, and scrolling all over pages to locate individual elements was not a long-term or robust solution. I had much better success using headless, and driver is able to locate all of my elements.
I used JavascriptExecutor and it worked
JavascriptExecutor executor = (JavascriptExecutor)browser;
WebElement submit_btn = browser.findElement(By.name("submit-btn"));
executor.executeScript("arguments[0].click()",submit_btn);
If selenium not able to identify web element due to resolution then try to move to the element using action.
WebElement element = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.visibilityOfElementLocated(By.id("someid")));
Actions action=new Actions(driver);
action.moveToElement(element).perform();
Then perform required action on the webelement
I'm clicking in on a button on a webpage using Selenium. The button creates a file which can be downloaded now. For this, a overlay is shown in Internet Explorer (yes, I HAVE to use this browser, it's a requirement).
Now I have to check the text on the overlay ("öffnen oder speichern" see my screenshot). I can imagine that it there is a solution using JavaScriptExecutor but I simply couldn't found a solution.
I also tried to find it in innerHTML-without success.
It's not an alert so I can't use Driver.switchTo().alert();
My Code still doesn't contain more than clicking on a button using XPath.
Actions action = new Actions(driver);
JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
String exportButtonXPath = generalHelper.getProperty("buttonCSVExportXPath");
WebElement exportButton = driver.findElement(By.xpath(exportButtonXPath));
action.click(exportButton).perform();
Do you have a solution how can test the text on this popup?
Actually, it is not related to the web browser any more. You need to interact with it as a desktop window.
->If you want to click it using selenium, you can locate its coordinates and use click by coordinates using selenium.
->If you want to accept to download it, you can find a capability to accept downloading by default (except IE).
->If you want to check the text value, for sure you've to automate it as desktop not as a web.
I can't click on element, because of overlay appears. Try to use capability to scroll to element and set it in bottom. Does not work for me.
ChromeOptions options = new ChromeOptions();
options.setCapability(CapabilityType.ELEMENT_SCROLL_BEHAVIOR, 1);
RemoteWebDriver driver = new ChromeDriver(options);
Can we do it in another way using java, chrome options (except js)?
chromedriver 2.36
selenium 3.11.0
testNG 6.14.2
If something overlays on top of the element you want to click then use actions method to move to the element which will make it enable to click and then click. This should work:
Actions actions1 = new Actions(driver);
actions1.moveToElement(youElement);
actions1.click();
actions1.build().perform();
I am using Java and Selenium to write tests for Chrome. Sometimes I need to get to chrome://downloads/ and click on CLEAR ALL button. I can get to the page by
RemoteWebDriver driver = (RemoteWebDriver) driverChrome;
driver.executeScript("window.open();");
Thread.sleep(500);
tabs = new ArrayList<String>(driverChrome.getWindowHandles());
driverChrome.switchTo().window(tabs.get(1));
Thread.sleep(500);
driverChrome.get("chrome://downloads/");
but I cannot click on the button, whatever xpath I use it says no such element
Below here JavascriptExecutor example to perform click on CLEAR ALL button using selenium :-
JavascriptExecutor executor = (JavascriptExecutor)driver
executor.executeScript("var dm = document.getElementsByTagName('downloads-manager')[0];var toolbar = dm.shadowRoot.getElementById('toolbar');var actions = toolbar.shadowRoot.getElementById('actions');actions.getElementsByClassName('clear-all')[0].click();");
Tested in Chrome Version 50.0.2661.102 m
Hope it will help you..:)
I am using FirefoxDriver webdriver. The page that loads in Firefox window is a large page and I want to scroll that page using selenium.
I want to know how this can be done.
If you want to scroll on the firefox window using selenium webdriver, one of the way is to use javaScript in the java code, The javeScript code to scroll down is as follows:
WebDriver driver = new FirefoxDriver();
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("window.scrollTo(0,Math.max(document.documentElement.scrollHeight," + "document.body.scrollHeight,document.documentElement.clientHeight));");
I think you should do something like
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
Good Luck.
Use this code to scroll single page down
Actions actions = new Actions(driver);
actions.sendKeys(Keys.BACK_SPACE).perform();
page.driver.browser.mouse.move_to( find("element").native,100,100)