How to press a "not visible" button with Selenium e Tor, but knowing the html ID?

Viewed 93

Using Selenium with WebDriverWait in Firefox, I would like to press the "Accept" button. On a normal browser the button screen is displayed normally, but the problem is that I am accessing the site with Tor, so the button screen appears dimmed/oscured with the shadow: it loads indefinitely and never displays. o it's like the button is never detected. In fact, in the Python console I get the error "selenium.common.exceptions.TimeoutException: Message" because the seconds pass but the button is not found. I also tried opening Tor Browser directly (without Python and codes) and there is the same thing: obscured button mask

I read that it is possible to press buttons even if it is not visible, by intervening directly on the code. How can I do? I repeat, with a normal browser the button is there, mind with Tor it is darkened. Possibly I would like to always use wait with WebDriverWait. I sincerely thank anyone who will help me. I will greatly appreciate any kind response

The html code of the button is onetrust-accept-btn-handler, to be precise it is: enter image description here

The page with the button with normal browser is:

enter image description here

The page with Tor and the blank/oscured screen is:

enter image description here

This is my code:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait

#CODE TOR CONNECTION
from selenium import webdriver
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
import os

torexe = os.popen('/home/mypc/.local/share/torbrowser/tbb/x86_64/tor-browser_en-US') 

profile = FirefoxProfile('/home/mypc/.local/share/torbrowser/tbb/x86_64/tor-browser_en-US/Browser/TorBrowser/Data/Browser/profile.default')
profile.set_preference('network.proxy.type', 1)
profile.set_preference('network.proxy.socks', '127.0.0.1')
profile.set_preference('network.proxy.socks_port', 9050)
profile.set_preference("network.proxy.socks_remote_dns", False)
profile.update_preferences()

firefox_options = webdriver.FirefoxOptions()
firefox_options.binary_location = '/usr/bin/firefox' 

driver = webdriver.Firefox(
    firefox_profile=profile, options=firefox_options, 
    executable_path='/usr/bin/geckodriver') 


#CODE CLICK BUTTON
driver.maximize_window()
driver.get("link")
WebDriverWait(browser, 20).until(EC.presence_of_element_located((By.CSS_SELECTOR, "onetrust-accept-btn-handler"))).click()

I used presence_of_element_located() which is the expectation for checking that an element is present on the DOM of a page. This does not necessarily mean that the element is visible.

I tried to use it too

 element = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[id='onetrust-accept-btn-handler']")))

or

WebDriverWait(driver,15).until(EC.element_to_be_clickable((By.XPATH,"//button[@class='onetrust-accept-btn-handler')]"))).click()

I always get the same mistakes with everyone, selenium.common.exceptions.TimeoutException: Message" because the seconds pass but the button is not found.

1 Answers

Try waiting for the element presence and then click it with JavaScript, not with Selenium. Like this:

WebDriverWait(browser, 20).until(EC.presence_of_element_located((By.ID, "onetrust-accept-btn-handler")))
driver.execute_script("document.getElementsById('onetrust-accept-btn-handler')[0].click()")

JavaScript is able to access invisible elements. This will not exactly simulate real user manual action via the GUI but it will work.

Related