1

Hello I wrote a function using selenium to click the "advisor" button so that I can scrape the table hidden. when I run it my chrome driver successfully opens and visits the page.. but the button does not get clicked. I hope, you guys help me to figure this out? NOTE: I am new to scraping techniques. also please let me know If this can be done with bs4. Here is a code:

def scrapper():
    u = "https://teqatlas.com/products-and-services/0chain"
    browser = webdriver.Chrome(executable_path=binary_path)
    wait = WebDriverWait(browser, 10)
    browser.set_page_load_timeout(10)
    # stop load after a timeout
    try:
        browser.get(u)
    except TimeoutException:
        browser.execute_script("window.stop();")
        
    button = browser.find_element_by_xpath('//button[@class="o5ph61-3 eBqrHG"]')
    if button:
        button.click()

scrapper() 

3 Answers 3

2
from selenium import webdriver
import pandas as pd
from selenium.webdriver.firefox.options import Options

options = Options()
options.add_argument('--headless')
driver = webdriver.Firefox(options=options)

driver.get("https://teqatlas.com/products-and-services/0chain")

btn = driver.find_element_by_css_selector("button.o5ph61-3.faMQuX").click()
df = pd.read_html(driver.page_source)[0]

df.to_csv("data.csv", index=False)

driver.quit()

Output: view-online

enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

let me install firefox, sorry
@SabbirTalukdar you can use Chrome, Edge, Firefox and Safari. check docs to download your preferred driver.
WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
@SabbirTalukdar you need to put geckodriver within your Python installation folder.
0

The xpath that you are using is incorrect. Please pick the below xpath to click on the button:

WebDriverWait(browser, 20).until(EC.presence_of_element_located((By.XPATH, "//button[text()='Advisories']"))).click()

You need 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

2 Comments

thank you so much, I will implement the code and will loet you know the result
@SabbirTalukdar please pick the updated ans, i have added explicit wait in this.
0

Does this work? I am also new to selenium. I changed find_element_by_xpath to find_element_by_class_name here though.

from selenium.webdriver.common.action_chains import ActionChains
def business_description_scrapper():
    u = "https://teqatlas.com/products-and-services/0chain"
    browser = webdriver.Chrome(executable_path=binary_path)
    wait = WebDriverWait(browser, 10)
    browser.set_page_load_timeout(10)
    # stop load after a timeout
    try:
        browser.get(u)
    except TimeoutException:
        browser.execute_script("window.stop();")

    button = browser.find_element_by_class_name("o5ph61-3.eBqrHG")
    if button:
        actions = ActionChains(browser)
        actions.click(button).perform()

business_description_scrapper() 

I tested it and the button got clicked.

7 Comments

thank you so much, I will implement the code and will loet you know the result
Does it find the button and unable to click? Or unable to find button?
a button got clicked but it was not the "advisories" button..
@Sabbir How about now?
this is the most troubling thing.. no error messages show up
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.