-3

I want to be able to cycle through this table's tabs and return the XPATHs and tab names.

I am using the python selenium webdriver.

This is the main container containing all the tabs from which i want the data

When i enter the website into selenium webdriver, i want it to first find this chart_table_container.

<div id="chart_table_container" class="tab_container ui-tabs ui-corner-all ui-widget ui-widget-content">

<ul class="tabs ui-tabs-nav ui-corner-all ui-helper-reset ui-helper-clearfix ui-widget-header" role="tablist">

Then I want it to cycle through the 3 tabs below, sometimes there might be more than 3. and the XPATH might be different, so I want to automate that.

Is there a way to cycle through the tabs below and return the XPATHs (//*[@id="ui-id-3"]) and also the name of the tabs (First Order) into a dictionary?

This is an example of one tabs

<li role="tab" tabindex="0" class="ui-tabs-tab ui-corner-top ui-state-default ui-tab ui-tabs-active ui-state-active" aria-controls="chart_wrapper_datatable_first_order" aria-labelledby="ui-id-3" aria-selected="true" aria-expanded="true">

<a href="#chart_wrapper_datatable_first_order" role="presentation" tabindex="-1" class="ui-tabs-anchor" id="ui-id-3">First Order</a></li>

This is another tab

<li role="tab" tabindex="-1" class="ui-tabs-tab ui-corner-top ui-state-default ui-tab" aria-controls="chart_wrapper_datatable_second_order" aria-labelledby="ui-id-4" aria-selected="false" aria-expanded="false">

<a href="#chart_wrapper_datatable_second_order" role="presentation" tabindex="-1" class="ui-tabs-anchor" id="ui-id-4">Second Order</a></li>

This is anothertab

<li role="tab" tabindex="-1" class="ui-tabs-tab ui-corner-top ui-state-default ui-tab" aria-controls="chart_wrapper_datatable_third_order" aria-labelledby="ui-id-5" aria-selected="false" aria-expanded="false">

<a href="#chart_wrapper_datatable_third_order" role="presentation" tabindex="-1" class="ui-tabs-anchor" id="ui-id-5">Third Order</a></li>

Does selenium have the capacity to do this? I looked through the documentation but I only found ways to search for the actual tabs using the XPATHs not find all the XPATHs at once.

undetected Selenium
  • 151,581
  • 34
  • 225
  • 281
anarchy
  • 2,560
  • 1
  • 9
  • 30
  • See: [How do I do X?](https://meta.stackoverflow.com/questions/253069/whats-the-appropriate-new-current-close-reason-for-how-do-i-do-x) The expectation on SO is that the user asking a question not only does research to answer their own question but also shares that research, code attempts, and results. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer! See also: [ask] – JeffC Sep 06 '19 at 14:42

2 Answers2

0

just try this:

tabs = driver.find_elements_by_xpath('.//li[@role= "tab"]/a') 

tabs = [[a.get_attribute("innerHTML"), "//*[@id=" + "".join(a.get_attribute("id")) + "]"] for a in tabs ]

If li classes is always the same, change xpath to [@class= "li class"] or [@role= "tab" and @class= "li class"]

brfh
  • 314
  • 1
  • 11
  • is there a way to add a wait to this because sometimes some of the elements dont get recorded, i think the page gets skipped before it fully loads – anarchy Sep 06 '19 at 19:12
  • is this correct? tabs = WebDriverWait(driver,5).until(EC.visibility_of_all_elements_located((By.XPATH,'.//li[@role= "tab"]/a'))) tabs = [[a.get_attribute("innerHTML"), "//*[@id=" + "".join(a.get_attribute("id")) + "]"] for a in tabs ] – anarchy Sep 06 '19 at 19:26
  • @anarchy, yeap, is looks like correct code. It will work fine. Or you can change wait condition to presence_of_all_elements_located – brfh Sep 06 '19 at 22:16
0

To print the table names such as First Order, Second Order, Third Order, etc you have to induce WebDriverWait for the visibility_of_all_elements_located() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    print([my_elem.get_attribute("innerHTML") for my_elem in WebDriverWait(driver, 5).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "div#chart_table_container>ul.ui-tabs-nav li>a.ui-tabs-anchor[id^='ui-id-']")))])
    
  • Using XPATH:

    print([my_elem.get_attribute("innerHTML") for my_elem in WebDriverWait(driver, 5).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@id='chart_table_container']/ul[contains(@class, 'ui-tabs-nav')]//li/a[@class='ui-tabs-anchor' and starts-with(@id, 'ui-id-')]")))])
    
  • Note : You have 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
    
undetected Selenium
  • 151,581
  • 34
  • 225
  • 281