I am currently running in headless
mode (uploaded in CirclCI Docker) and the site I am working on has an Accept Cookies banner. Apparently, in headless mode, it obscures the Log In button I need to click to be able to proceed with my test. Is there a way to disable checking?
We have .click({force: true})
in Cypress. Do we have a counterpart in Selenium?
Website is https://mailtrap.io/
Backstory:
Before going it Mailtrap, I came from another origin. I tried to use the Options()
for Firefox before visiting Mailtrap but it doesn't work. If I put it at the first run, it messes up with the other site as well.
Here is the code:
options = Options()
options.set_preference("network.cookie.cookieBehavior", 2)
context.driver.get("https://mailtrap.io/signin")
loginButton = context.mailTrapPage.getLoginButton()
context.driver.execute_script("arguments[0].click();", loginButton)
Here is the error I'm getting:
selenium.common.exceptions.ElementClickInterceptedException: Message: Element <a href="#"> is not clickable at point (388,614) because another element <p > obscures it
Stacktrace:
RemoteError@chrome://remote/content/shared/RemoteError.jsm:12:1
WebDriverError@chrome://remote/content/shared/webdriver/Errors.jsm:192:5
ElementClickInterceptedError@chrome://remote/content/shared/webdriver/Errors.jsm:291:5
webdriverClickElement@chrome://remote/content/marionette/interaction.js:166:11
interaction.clickElement@chrome://remote/content/marionette/interaction.js:125:11
clickElement@chrome://remote/content/marionette/actors/MarionetteCommandsChild.jsm:204:29
receiveMessage@chrome://remote/content/marionette/actors/MarionetteCommandsChild.jsm:92:31
CodePudding user response:
Yes, you can click any existing element, including covered by other element, with JavaScript click as following:
element = driver.find_element(By.XPATH, element_xpath_locator)
driver.execute_script("arguments[0].click();", element)
However you need to remember that Selenium is developed to imitate actions performed by human user via GUI and human user can not perform such actions. As a user you will have to scroll the page or open dropdown etc. to make that element accessible and only then click it. But if sometimes you just need to perform some action without caring if such action can be done by a human user - you can use JavaScript clicks and other methods, out of Selenium covered actions.
UPD
You specific problem caused by selecting a wrong element.
The following code works for me:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("start-maximized")
webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(service=webdriver_service, options=options)
url = 'https://mailtrap.io/signin'
driver.get(url)
wait = WebDriverWait(driver, 20)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "#signinTopBlock a[href*='signin']"))).click()