Click on a Selenium div

Asked

Viewed 258 times

-1

I need to click the 'LIVE NOW' button on the 'https://www.academiadasapostasbrasil.com/' page. I made the following script, but it is giving an error, could not be scrolled into view. Below is the code.

import requests
from bs4 import BeautifulSoup
from selenium import webdriver

url = 'https://www.academiadasapostasbrasil.com/'

web_r = requests.get(url)
web_soup = BeautifulSoup(web_r.text, 'html.parser')

driver = webdriver.Firefox()
driver.get(url)

elemento = driver.find_element_by_xpath('//div[@id="fh_live_tab"]').click()


1 answer

1

Two things:

  1. First you need to use a scroll to scroll until the button you want to click appears.

    driver.execute_script("window.scrollTo(0, num)")

in a vc puts the number of pixel you want the page to roll example:

driver.execute_script("window.scrollTo(0, 320)")
  1. According to the tag html and the id were incorrect to tag correct is a and the id correct is show_tab3

    elemento = driver.find_element_by_xpath('//a[@id="show_tab3"]').click()

  • Thank you very much, Juan Caio. It worked, but now I have another problem. I wanted the HTML code after the click of the button to be sent to me. However, when I return the HTML, I just return the old HTML, without clicking the button. Can you tell me any alternative to get the HTML that is after clicking on the button 'LIVE NOW'? Thank you.

  • To help you better, as you are returning this html?

  • I am using the following command to return html. html = driver.execute_script("Return Document.documentElement.outerHTML") sel_soup = Beautifulsoup(html, 'html.parser')

  • You can do the following: content = driver.page_source soup = BeautifulSoup(content,'lxml') Ai vc can get the correct div: dados = soup.find("div", attrs={"id":"fh_live_tab"}) E then you can take the table: ;tabela = dados.find('table', attrs={"class":"competition-today dskt"})

  • Juan, the data variable is coming without the content inside. Thus, the table returns None. [<div class="Boxed tabcontent games_wrapper" id="fh_live_tab" style="opacity: 0.911494; display: block;"><div id="Loader-wrapper" style="height: 80px; width: 642px;"><div class="load-overlay" style="width: 642px; height: 80px;"><div class="ajax-loading big"></div></div></div></div></div>>]

  • Heuller look at this code here that I performed as a test and compare with your code.

  • Juan, I don’t know why, but is returning me None to the table. I can contact you via some social network?

  • You can talk to me on Linkedin.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.