Get element by xpath with Selenium

Viewed 37

I need to get Id information by xpath using selenium for this html structure:

<div class="content">

<ul>
                                                                                                                        <li class="info-holder">
<h3 style=""><a href="#" style="">Information</a></h3>
<div class="tabela-lista-valor">
<p class="outer tabela-lista-item"> Id </p>
<p class="inner" style="font-family: 'Open Sans', Arial, Helvetica, sans-serif !important;margin-bottom: 0px;padding: 8px 8px;">1</p>
</ul>
</div>

But with using selenium to retrieve the data I receive this message:

TypeError: 'WebElement' object is not subscriptable

This is my code:

for z in range(2):
    id_test = navegador.find_element(By.XPATH,"(//p[contains(concat(' ', normalize-space(@class), ' '),' inner ')])[1]")
    for i in range(1,500): 
        dados_temporarios = {'ID': id_test[i].text}
        dados.append(dados_temporarios)
    navegador.find_element(By.XPATH, "//a[contains(text(),'next')]").click()
1 Answers

The problem is your use of the find_element method, because afterwords you attempt to access an index of the variable, when it is a single element in length.

What you are looking for is the find_elements command, which will return a list from which you can select individual elements. For more information, see the Selenium documentation on this topic here.

Related