MRHT
MRHT

Reputation: 35

Finding HTML-element based on value

I'm trying to grab and click on a link by using selenium's .find_element_by_class_name() from the following html:

<table id="article-table" class="table__full--secondary table__list--articles">
    <tbody><tr>
      <th>Förpackningsinnehåll</th>
      <th>Varunummer</th>
      <th>Produktkod</th>
      <th>NILPL packid</th>
      <th>Marknad</th>
      <th>Foretag</th>
      <th>Rec</th>
        </tr>
          <tr name="sokTraffArtikel_2020071500" class="table__list-item--secondary" data-href="visaArtikel.xhtml?id=202010071000" title="Go to">
            <td class="js-breakword" data-max-word-length="30">
              <a class="link-tab-focus" href="javascript:;">Blister, 30 tab</a>
            </td>
            <td>587100</td>
            <td>07350096049645</td>
            <td>202010071001985</td>
            <td name="marknadsfordArtikel">Ja</td>
            <td></td>
            <td>Ja</td>
          </tr>
          <tr name="sokTraffArtikel_20200715000027" class="table__list-item--secondary article-highlight" data-href="visaArtikel.xhtml?id=20201007100348" title="Go To">
            <td class="js-breakword" data-max-word-length="30">
              <a class="link-tab-focus" href="javascript:;">Blister, 50 tab</a>
            </td>
            <td>566567</td>
            <td>07350096049623</td>
            <td>202010071088665</td>
            <td name="marknadsfordArtikel">Ja</td>
            <td></td>
            <td>Ja</td>
          </tr>
      </tbody></table> 

By using the following code I'm able to click on a link by using the find_element_by_class("link-tab-focus") from :

<a class="link-tab-focus" href="javascript:;">Blister, 30 tabletter</a>

What I can't figure out or find any answers to is if it's possible to click on a specific class, depending on the values from the "<td>":s below. Say the value I'm looking for is "07350096049623", is there any way I can select the link-tab-focus in the td-class "js-breakword" that hosts the value I'm looking for? Is it even possible to grab an href (from the a-class) based on value?

Upvotes: 1

Views: 67

Answers (1)

cruisepandey
cruisepandey

Reputation: 29382

you will have to use xpath in that case :

let's say you want to extract a href based on this value 07350096049623 form td.

//td[contains(text(), '07350096049623')]/../descendant::a

and use it like this :

a_href_value = find_element_by_xpath("//td[contains(text(), '07350096049623')]/../descendant::a").get_attribute('href')
print(a_href_value)

or if you just want to click on a tag based on td :

then do this :

a_href_value = find_element_by_xpath("//td[contains(text(), '07350096049623')]/../descendant::a")
a_href_value.click()

Upvotes: 1

Related Questions