Selenium cannot click button because there is a div overlay - python

I am trying to scrap this webpage https://www.tecnocasa.es/venta/piso/barcelona/barcelona/510567.html, the code i use is the following, and i think is correct:
options = Options()
options.headless = False
driver = webdriver.Firefox(options=options, executable_path=r'geckodriver.exe')
driver.get(url)
# ("Headless Firefox Initialized")
#dins del id= cookie-banner
frame = driver.find_element(by=By.CSS_SELECTOR,value = '[id = "cookie-banner"]')
print(frame)
buttons = frame.find_elements(by=By.TAG_NAME, value='button')
print(buttons)
buttons[0].click()
time.sleep(5)
The buttons are found but when i try to click one i have the error <button class="btn-default"> is not clickable at point (1138,829) because another element <div id="hide-overlay" class="hide-overlay fade-leave-active fade-leave-to"> obscures it
How can i disable this overlay and click the button to accept the conditions of the page?

Your code works without any problem on my computer, try this one
driver.find_element(By.CSS_SELECTOR, '#close').click()
If also this will not work, you can try this code which hides the banner. However, you should run it each time that a new page is loaded
overlay = driver.find_element(By.CSS_SELECTOR, 'div.cookies-overlay')
driver.execute_script("arguments[0].style.display = 'none';", overlay)
banner = driver.find_element(By.CSS_SELECTOR, '#cookie-banner')
driver.execute_script("arguments[0].style.display = 'none';", banner)

Related

Waiting for a page element to load unless error pop-up (modal) using Selenium

I'm using Python Selenium to fill a form. When I click a button like this:
Button = driver.find_element(By.XPATH, '//*[#id="root"]/div[1]/form/button')
Button.click()
img_wait = WebDriverWait(driver, timeout).until(EC.visibility_of_all_elements_located((By.CLASS_NAME, "image")))
img = driver.find_elements(By.CLASS_NAME, "image")
What happens is that sometimes the website doesn't process the request correctly and opens up a modal with an error message (CLASS_NAME "alert").
What I would like to do is, while waiting for the class "image" to load, if any element with class "alert" gets loaded by the page, hit the button again. Otherwise just keep waiting (I have a timeout exception anyway at the end).
Incase the website doesn't process the request correctly and opens up the modal with an error message (CLASS_NAME "alert") to click on the button again you can wrap up the search for the alert within a try-except{} block as follows:
Button = driver.find_element(By.XPATH, '//*[#id="root"]/div[1]/form/button')
Button.click()
try:
WebDriverWait(driver, timeout).until(EC.visibility_of_all_elements_located((By.CLASS_NAME, "alert")))
print("Alert was found")
driver.find_element(By.XPATH, '//*[#id="root"]/div[1]/form/button')
except TimeoutException:
print("Alert wasn't found")
img_wait = WebDriverWait(driver, timeout).until(EC.visibility_of_all_elements_located((By.CLASS_NAME, "image")))
img = driver.find_elements(By.CLASS_NAME, "image")
I've actually found a solution:
img_wait = WebDriverWait(driver, timeout).until(lambda x: x.find_elements(By.CLASS_NAME, "image") or x.find_elements(By.CLASS_NAME, "alert"))[0]
alert = driver.find_elements(By.CLASS_NAME, "alert")
This way I can use an if like this:
if alert:
print("alert found")
Button.click()
img_wait = WebDriverWait(driver, timeout).until(EC.visibility_of_all_elements_located((By.CLASS_NAME, "image")))
image = driver.find_elements(By.CLASS_NAME, "image")
else:
image = driver.find_elements(By.CLASS_NAME, "image")
which prevents stale elements as well

Q: Can't keep clicking button by chorme web driver in python

Question:
How can I show all reviews by clicking "Show more reviews" button?
What did I do:
To scrape all reviews, I decided to Keep clicking until that button disappears.
But some new reviews didn't appear after clicking for 8 times (using while below).
I already checked xpath of the button but it didn't changed.
If I click the button manually without the driver, I can see new reviews.
import time, random
from selenium import webdriver
from latest_user_agents import get_random_user_agent
### ser user_agent
user_agent = get_random_user_agent()
options = webdriver.ChromeOptions()
options.add_argument("--user-agent=" + user_agent)
driverpath = "C:/Users/~~/chromedriver.exe"
driver = webdriver.Chrome(chrome_options=options,executable_path=driverpath)
### target url
url = "https://www.sony.co.uk/electronics/truly-wireless/wf-l900/reviews-ratings"
### open URL
driver.get(url)
time.sleep(5)
# accept cookies
driver.find_element_by_xpath('//*[#id="onetrust-accept-btn-handler"]').click()
# show all reviews
try:
while True:
driver.execute_script('window.scroll(0,1000000);')
driver.find_element_by_xpath('//*[#id="reviews_listing_278405943492055451029662"]/div[3]/div[4]/button').click()
time.sleep(random.randrange(2, 5, 1))
except:
print("---------------- finish showing all reviews ----------------")
Try going to the end of the webpage and then do it as the button might not been loaded fully.
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
You might also try to press the button only when it is available.
element = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.ID, "myElement")))
You can try javascript click:
element = driver.find_element_by_xpath('//*[#id="reviews_listing_278405943492055451029662"]/div[3]/div[4]/button')
driver.execute_script("arguments[0].click();", element)
In this method, the scrolling shouldn't be necessary. See more info about the subject here.

How can I automatically click on the text "Show more matches"? My code doesn't work

There is a text button that says "Show more matches", it is used to scroll down the page, because no more results come out. I would like to press it automatically, but there is something wrong with my code. Can you help me please?
IMPORTANT: It is not yet on this page, but soon there will be a second button "Show more meetings", because I scroll down and find "Show more meetings", then in a few months if I go down even further and find a second button "Show more matches". In the page there is no second button yet, but I would like to make it press that too (it's the same same button, so I don't think it's complicated).
So I would like to press 2, but also 3 the same, at the same time
P.S: The purpose of the request and the code is only for personal study reasons, so for personal didactic reasons, no profit. This question and this code is not for commercial or profit-making purposes.
driver.get("url")
driver.implicitly_wait(12)
wait = WebDriverWait(driver, 12)
try:
wait.until(EC.element_to_be_clickable((By.CLASS_NAME, "a[class^='event__more event__more--static']"))).click() #because another element <div class="skOT skOT--ot"> obscures it
except Exception as ex:
print('EX:', ex)
UPDATE:Before driver.get and the link, there is this
torexe_linux = os.popen('/home/xxxxx/.local/share/torbrowser/tbb/x86_64/tor-browser_en-US')
profile = FirefoxProfile('/home/xxxx/.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) #certi la tengono True
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')
You need to close accept cookies panel.
Show more button appears on the bottom of the page, you need to scroll it to the view in order to click it.
After clicking it you need to wait for the page loading in order to get that element again, scroll to it again and the click it again.
As following:
from selenium.webdriver.common.action_chains import ActionChains
driver.get("link")
driver.implicitly_wait(12)
wait = WebDriverWait(driver, 12)
actions = ActionChains(driver)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "button#onetrust-accept-btn-handler"))).click()
while(driver.find_elements_by_css_selector('a.event__more.event__more--static')):
show_more = driver.find_element_by_css_selector('a.event__more.event__more--static')
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
actions.move_to_element(show_more).perform()
time.sleep(0.5)
show_more = driver.find_element_by_css_selector('a.event__more.event__more--static')
show_more.click()
time.sleep(3)

Selenium Locating Unclickable Element

I'm trying to locate a element, but I can't click on it. The id is ("save-all-conditionally"), but it's not working if I click on it. I tried css.selector, xpath and all other things, but nothing is working!
There are 2 frames, frame inside frame.
You would need to switch to parent frame then child frame.
here is the working code :
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.maximize_window()
driver.get("https://www.gmx.net/consent-management/")
driver.implicitly_wait(10)
firstFrame = driver.find_element_by_xpath("//iframe[#class='permission-core-iframe']")
driver.switch_to.frame(firstFrame)
driver.switch_to.frame(0)
driver.find_element_by_xpath("//button[#id='save-all-conditionally']").click()
Try this code, see if it works:
ele = driver.find_element_by_xpath("//*[#id='save-all-conditionally']")
ele.click()
Not tested yet:
iframe = driver.find_elements_by_tag_name('iframe')
if len(iframe) == 1:
driver.switch_to.frame(iframe[0])
button = driver.find_element_by_id('save-all-conditionally')
if button:
button.click()
Since the iframe has no id get it by tag name, it should be one iframe use switch_to.frame to switch to the iframe content and get the button to click on it

Selenium Python - Can't click on element

I'm trying to selenium click on the "next" button at the bottom of the page (just as shown by cursor in image)
Here's the link to full web: http://hr.jsbchina.cn/zp/trs/hotPostList.do
I've tried three methods:
1) The conventional click
nextbutton = browser.find_element_by_xpath('/html/body/form/table[5]/tbody/tr/td[2]/a')
nextbutton.click()
browser.implicitly_wait(10)
2) The "element_to_be_clickable"
wait = WebDriverWait(browser, 10)
wait.until(EC.element_to_be_clickable((By.XPATH, '/html/body/form/table[5]/tbody/tr/td[2]/a'))).click()
time.sleep(10)
3) I've also tried adding the page number to the page "text field" and clicking on "Go". And I've tried resizing window size as follows:
browser.set_window_size(1920, 1080)
browser.implicitly_wait(10)
Are there any other methods to click on "next"? Because the rest can't seem to work.
Below locators should help you.
driver.find_element_by_link_text('next')
OR
driver.find_element_by_partial_link_text('next')

Categories

Resources