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

Time:11-18

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.

enter image description here

driver.get("https://www.diretta.it/calcio/svezia/allsvenskan/risultati/")
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 > 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')   

CodePudding user response:

  1. You need to close accept cookies panel.
  2. Show more button appears on the bottom of the page, you need to scroll it to the view in order to click it.
  3. 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("https://www.diretta.it/calcio/svezia/allsvenskan/risultati/")
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)
  • Related