[Python] How to click on a "Data Only" tab using Selenium in Python?

Discussão em 'Python' iniciado por Stack, Novembro 29, 2024.

  1. Stack

    Stack Membro Participativo

    I'm trying to automate interaction with a webpage using Selenium in Python. The page contains a tab bar, and I need to click on the "Data Only" tab. The element has the following HTML structure:

    <div id="tabs" class="tabContainer2L">
    <div class="tab2L" id="_xdotabsregion2">
    <div class="tabDiv2L">
    <div class="tabDivA2L" id="_xdotabsregion3">
    <div class="tabStartSpace2L"></div>
    <div class="tabStartA2L"></div>
    <div class="masterTabBarTabSecondarySelected tabBGA2L">
    <a class="tabLink2LSelected" href="javascript:void(0)" title="Data Only">Data Only</a>
    </div>
    <div class="tabMiddleAN2L"></div>
    <div class="masterTabBarTabSecondaryEnabled tabBGN2L">
    <a class="tabLink2L" href="javascript:void(0)" title="PDF">PDF</a>
    </div>
    <div class="tabEndN2L"></div>
    </div>
    </div>
    </div>
    </div>


    What I've Tried: CSS Selector: I’ve tried to use CSS_SELECTOR to target the "Data Only" link:

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC

    driver = webdriver.Chrome()

    driver.get('your-webpage-url')

    wait = WebDriverWait(driver, 20)
    data_only_element = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'a[title="Data Only"].tabLink2LSelected')))
    data_only_element.click()

    driver.quit()


    This doesn't seem to work, and the TimeoutException is thrown. XPath: I also tried XPath:

    data_only_element = wait.until(EC.element_to_be_clickable((By.XPATH, "//a[@title='Data Only' and @class='tabLink2LSelected']")))
    data_only_element.click()


    But still, I’m facing issues with clicking the element.

    Problem:

    • I’m encountering a TimeoutException, and the element isn't clickable. How can I make sure that I can click on the "Data Only" tab properly?
    • I suspect there might be other reasons the element isn't clickable, but I’m not sure if it’s related to page load timing, iframes, or other factors. Environment:
    • Selenium 4.0
    • Python 3.x
    • Google Chrome Could someone point out what I might be doing wrong and how I can click on the "Data Only" tab?

    Continue reading...

Compartilhe esta Página