0

I have a code to automatically click on the "Show more" button at the bottom of a page with Selenium e Firefox with proxy TOR, but I get an error:

     raise TimeoutException (message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:

enter image description here

The code seems to be written well, I don't understand what the problem is. For greater clarity, I also share the connection with the proxy I use (everything ok, works fine) and then the code to click on the button automatically where I have the error. Can you help me please? Thanks

P.S: The code was set to click several times on the "Show more" button, because if you click on "Show more" the first time, then the page scrolls further down, but then I get another second "Show more" button. Sometimes even a third "Show more". So I would also like to click on the second and third "Show more" when they are loaded.

UPDATE: the cookie screen is shady, shaded, almost transparent black, so maybe that's why your code isn't working. Maybe the Tor connection prevents the normal display of cookies and you can't press the button (I think, maybe, I don't know)

enter image description here

Code for connect Firefox with Proxy Tor

from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
import os
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait

#Connect Firefox with Proxy Tor
torexe_linux = os.popen('/home/xxxx/.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')   

driver.get("link")
driver.maximize_window()

Code for automatic click (THE PROBLEM IS HERE)

from selenium.webdriver.common.action_chains import ActionChains

    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)
Erling Olsen
  • 1
  • 4
  • 15
  • That means it's going away and there is no more show more button. – Arundeep Chohan Nov 22 '21 at 21:39
  • @ArundeepChohan what do you mean? the button is literally called "Mostra più incontri". Can you help me solve it please? Thanks – Erling Olsen Nov 22 '21 at 21:51
  • If you click it multiple times it stops showing up and no longer appears. Then your code time outs. Just wrap it in a try except and then break. – Arundeep Chohan Nov 22 '21 at 22:18
  • @ArundeepChohan I made sure that you click several times, because every time you click on "Show more", the page scrolls even lower but then another "Show more" appears. As I'm new to Python, could you show me the code in an answer please? Of course I will vote for you. Thanks – Erling Olsen Nov 22 '21 at 22:36

2 Answers2

1

Possibly the Show more button no more shows up as all the records are already being shown. In those cases, an ideal solution would be to:

  1. Scroll the required height.

  2. Move to the webelement. (this step isn't mandatory)

  3. Click on Show More inducing WebDriverWait

  4. Wrap up the code in a try-except{} block

  5. Your optimum code block will be:

    WebDriverWait(driver, 12).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#onetrust-accept-btn-handler"))).click()        
    while True:
        try:
            driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
            ActionChains(driver).move_to_element(WebDriverWait(driver, 12).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "ba.event__more.event__more--static")))).perform()
            WebDriverWait(driver, 12).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.event__more.event__more--static"))).click()
            print("Show more button clicked")
            continue
        except TimeoutException:
            print("No more Show more buttons")
            break
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I thank you so much. A curiosity: the code was set to click several times on the button "Show more", because if you click the first time on "Show more", then the page scrolls further down, but then I receive another second "Show more" button . Sometimes even a third "Show more". The code you wrote you click only once on "Show more"? I would also like to click on the second and third "Show more" when they are loaded. Thanks – Erling Olsen Nov 22 '21 at 22:53
  • The code was to click on _`Show more`_ button till it shows up. However, the code logic is bound to differ as per the underlying conditions which builds up the DOM. One improvement, instead of `scrollTo()`, try to use `scrollInToView()` for some _visible element_ so that the _`Show more`_ button comes within the viewport. – undetected Selenium Nov 22 '21 at 23:08
  • 1
    I tried your code, but I'm still getting the same error message. Do you have any other ideas? Thanks – Erling Olsen Nov 22 '21 at 23:11
  • See the above comment, a couple of variations you gotta try. – undetected Selenium Nov 22 '21 at 23:12
  • 1
    I haven't viewed your comment yet, sorry. I've tried scrollInToView (), but I'm still getting the same error :( – Erling Olsen Nov 22 '21 at 23:18
  • Out of respect I voted for your answer, but I haven't solved the problem. I don't know if you've also tried the code using my Firefox connection with Tor proxy, but you may notice that the cookie screen is shady, shaded, almost transparent black, so maybe that's why your code isn't working. Maybe the Tor connection prevents the normal display of cookies and you can't press the button (I think, maybe, I don't know). Do you have ideas on how to solve? Thanks – Erling Olsen Nov 23 '21 at 17:19
  • @pmadhu Out of respect I voted for your answer, but I haven't solved the problem. I don't know if you've also tried the code using my Firefox connection with Tor proxy, but you may notice that the cookie screen is shady, shaded, almost transparent black, so maybe that's why your code isn't working. Maybe the Tor connection prevents the normal display of cookies and you can't press the button (I think, maybe, I don't know). Do you have ideas on how to solve? Thanks – Erling Olsen Nov 23 '21 at 17:20
1

Try like below.

Use find_elements to store the Show more element in a list. Then compare the length of the list to 0, to determine if the Show more button is available to click.

driver.get("URL")

wait = WebDriverWait(driver,30)

# Accept Cookies
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"button#onetrust-accept-btn-handler"))).click()

while len(driver.find_elements(By.CSS_SELECTOR,"a.event__more.event__more--static")) > 0:
    showmore = driver.find_element(By.CSS_SELECTOR, "a.event__more.event__more--static")
    driver.execute_script("arguments[0].scrollIntoView(true);", showmore)
    showmore.click()
    time.sleep(2)
pmadhu
  • 3,373
  • 2
  • 11
  • 23
  • It does not work – Erling Olsen Nov 23 '21 at 16:00
  • Sorry, but I'm getting an error from your code. I get str 'object has no attribute' sleep '. How can I solve? Thanks P.S: I removed the green tick in the answer just to ask you, then I put it back. Thanks – Erling Olsen Nov 25 '21 at 02:09
  • @heovan1999 - If its because of `time.sleep(2)` then you need to `import time`. – pmadhu Nov 25 '21 at 02:11
  • I already use import time, but I still get this problem – Erling Olsen Nov 25 '21 at 02:18
  • For privacy and security reasons can you remove the link from your answer please? Maybe replace it with the word "link" or whatever you want. Sorry for the request. I thank you in advance. P.S: I removed the green check from the answer just to write to you, then I will put it again after you have given me the confirmation that the link has been removed, so that in the meantime we can continue writing. Thanks – Erling Olsen Nov 25 '21 at 22:23