0

I'm trying to test a website with Selenium by clicking on an element. The element is on the page but I cannot see it because it is covered with Selenium floating alert "Chrome is being controlled by automated test software". So I cannot click on my element. One of the ways to solve the problem and get access to the element is to zoom out of the page.

When I use code

browser.execute_script("document.body.style.zoom='40%'")

the page zooms out well. But the element I wanted to reach is stuck at the top of the page and not accessible for clicking anyway.

I expected to zoom out the page by pressing a combination of keys Ctrl+'-' with Selenium but failed to get any result. I tried this code.

import time
from selenium import webdriver
from selenium.webdriver import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By

with webdriver.Chrome() as browser:
    browser.get('https://some_address/index.html')
    time.sleep(1)
    element=browser.find_element(By.ID, 'some_text')
    actions=ActionChains(browser)
    actions.key_down(Keys.CONTROL,element) \
        .key_down(Keys.SUBTRACT,element).key_up(Keys.SUBTRACT,element) \
        .key_down(Keys.SUBTRACT,element).key_up(Keys.SUBTRACT,element) \
        .key_up(Keys.CONTROL,element).perform()

But zoom out does not work.

# ---------------------------------

One of the solutions to this problem was suggested by @ryyyn.

browser.execute_script("arguments[0].click();", element)

# ---------------------------------

Another solution to the issue is to remove the floating "Chrome is being controlled by automated test software" alert by adding of an option in the code as follows: options.add_experimental_option("excludeSwitches", ["enable-automation"]) And the code includes a few more strings:

from selenium.webdriver.chrome.options import Options

options = Options()
options.add_experimental_option("excludeSwitches", ["enable-automation"])

with webdriver.Chrome(options=options) as browser:
    ...

This code completely eliminates the floating alert and therefore makes an element available for clicking.

4
  • I don't understand the problem you are trying to solve here. Why would the scroll bar disappearing being an issue? And did you actually try clicking on the elements first? Usually, Selenium scrolls them into view automatically. Commented Jan 22 at 21:37
  • please edit your question and add more details such as the url or the html content in order to get you the help . stackoverflow.com/help/minimal-reproducible-example Commented Jan 23 at 1:29
  • 1
    Selenium's keyboard input can't be used for hotkey macros like CTRL +/-... Instead you can directly call the element's click function: browser.execute_script("arguments[0].click();", element). This doesn't care that the element is non-interactible (outside the viewport etc). Also, as a personal suggestion I recommend Playwright over Selenium for a much easier time doing web automation. (In Playwright, you would just use element.click(force=True)) Commented Jan 24 at 17:35
  • Thanks a lot! This code browser.execute_script("arguments[0].click();", element) really works! Commented Jan 27 at 12:34

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.