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.
keyboardinput can't be used for hotkey macros likeCTRL +/-... Instead you can directly call the element'sclickfunction: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 useelement.click(force=True))