Skip to content Skip to sidebar Skip to footer

Unable To Get Table Element In Website Using Selenium

The website below has several tables, but my code is not being able to get a specific one (nor any other table). The code aims to get data from table 'Ações em Circulação no Me

Solution 1:

You wrote XPATH in CSS selector definition. You should locate tables = browser.find_elements_by_css_selector('.responsive') if you want all tables, and then parse from them. OR Use browser.find_element_by_xpath(.//*[@id='div1']/div/table) to locate exact table.

Solution 2:

One quick correction you can make is to change this content = browser.find_element_by_css_selector('//div[@id="div1"]') to content = browser.find_element_by_xpath('//div[@id="div1"]') because it actually is an xpath you're using.

The reason the second attempt is not working might be that the div1 element is not scrolled into view. Selenium does not interact well with elements that are not visible. So try this:

element = browser.find_element_by_xpath('//*[@id="div1"]')
# Force the element to be scrolled into view, even if you don't need its location.location = element.location_once_scrolled_into_view
# Now Selenium can get its text.text = element.text

Solution 3:

To locate the WebElement and extract the text Pessoas Fisicas you can use the following line of code :

content = driver.find_element_by_xpath("//h3[.,'Ações em Circulação no Mercado']//following::div[1]//table[@class='responsive']//tr//following-sibling::td[1]").get_attribute("innerHTML")

Update (no code change)

The xpath expression :

//h3[.,'Ações em Circulação no Mercado']//following::div[1]//table[@class='responsive']//tr//following-sibling::td[1]

Shouldn't be within single quotes e.g. 'xpath_here'. Put the xpression with in double quote e.g. "xpath_here"

See the working snapshot :

tds

Post a Comment for "Unable To Get Table Element In Website Using Selenium"