How to bypass cookiebot.com uc.js by Selenium Python - python

I need to find a way to click on the cookie agreement button created by the javascript code that is provided by cookiebot.com such as the following example in a HTML code,
<script id="Cookiebot" src="https://consent.cookiebot.com/uc.js" data-cbid="8123486-d5f-ec" data-blockingmode="auto" type="text/javascript"></script>
I have searched the net, but there is no example showing how to do that using Selenium Python.

To click() on the element Allow all you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
driver.get("https://www.cookiebot.com/en/")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#CybotCookiebotDialogBodyLevelButtonLevelOptinAllowAll"))).click()
Using XPATH:
driver.get("https://www.cookiebot.com/en/")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[#id='CybotCookiebotDialogBodyLevelButtonLevelOptinAllowAll']"))).click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Related

How to use driver.find_element() to locate an element and click on it using Selenium

I want to ask aobut driver.find_element(). I want to make the automatic site login with a chrome driver and python. I want to click the login button, but it doesn't work.
Here is code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time
driver = webdriver.Chrome()
driver.get('https://www.naver.com')
time.sleep(2)
search = driver.find_element(By.CLASS_NAME,'link_login')
search.click()
I also used
search = driver.find_element(By.CLASS_NAME,'link_login')
but it didn't work too. How can I make it to work?
You were close. To click on the clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using CLASS_NAME:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CLASS_NAME, "link_login"))).click()
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.link_login"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='link_login']"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Unable to locate element with id, class_name and xpath using Selenium Python

How to click this button:
I have tried the following:
sumbitbutton = driver.find_element(By.XPATH, "//div[text() = 'mt8 mb8']")
I decided to insted abandon this method and go for a nuther as it lead to a lot of other problums latter down the line
You will want something like:
button = WebDriverWait(browser, 20).until(EC.presence_of_element_located((By.CSS_SELECTOR, "inputButton.main_submit")))
button.click()
As you did not confirm the URL, I cannot test it, but it should work.
Also, do not forget to import
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Generally <div> tags aren't clickable. Additionally mt8 mb8 are the classanmes, not the text.
Solution
As per the HTML:
to click on the <input> element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.inputButton.main_submit[value='Submit']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='inputButton.main_submit' and #value='Submit']"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Python, Selenium: how to click on javascript:void(0) element

I have an "onclick" element in a webpage,
<a href="javascript:void(0)" onclick=
"toIndexHtml('3','http://xxxxxxxxxxx/trade','0')">
<i></i>
<span></span>
trade
</a>
It is shown in the webpage as a button and I want to click on it, I tried to search for it using the following code:
driver.find_element_by_xpath("//a[contains(#onclick,'toIndexHtml')]").click()
WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH,"/html/body/div/ul/li[3]/a"))).click()
Both are not working, please suggest if there is any other ways! Thanks in advance!
P.S.: I am using Chrome WebDriver and Chrome v64.
Your first locator looks perfect and should have worked.
Ideally, to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[onclick^='toIndexHtml']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[starts-with(#onclick,'toIndexHtml')]"))).click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Python Selenium - How do I check if we have an item with this on HTML?

I need to check if we something with on the page as a trigger:
data-id="false_5511971198499#c.us_DBC6E6D07C76B179C26A40D689B2AEB2"
But, I need to check only "data-id="false_" because the rest could be anything.
I tried to create:
element = driver.find_element_by_xpath("[data-id='False']")
But didn't worked. And I don't know how to continue like if there is this element we need to print(ok).
Could you help me?
Thank you!
It's a dynamic element so you have to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:
Using XPATH:
WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.XPATH, "//*[starts-with(#data-id, 'false_')]")))
Using CSS_SELECTOR:
WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "[data-id^='false_']")))
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Try this
//*[starts-with(#data_id,'false_')]

How to click on the webelement with in the highlighted script using Selenium and Python

I tried:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,"//*[#value='Sign Out']")))
but no luck.. please see image for the html script
The desired element is a JavaScript enabled element so to click on the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR and click():
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "form[action*='Logoff']>li>input[value='Sign Out']"))).click()
Using XPATH and submit():
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//form[contains(#action, 'Logoff')]/li/input[#value='Sign Out']"))).submit()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Categories