parik
parik

Reputation: 2415

How to click a link whose href contains some string in Selenium Python?

I want to click on a link who has "930" in his url for this site

<div id="liste_drives">
    <ul>
    <li class="">
        <span class="left">
        02 -
        <a href="http://www.tt.com/d/n/p/st-985">tt</a>
        <p class="a"> </p>
        </span>
        <a class="entre"  href="/d/m/St-985/">i enter here</a>  
    </li>
    <li class="">
        <span class="left">
        03 -
        <a href="http://www.tt.com/d/n/p/ee-930">tt</a>
        <p class="a"> </p>
        </span>
        <a class="entre"  href="/d/m/ee-930/">i enter here</a>
    </li>
</div>

i read this answer

and i try this :

id = "930"
driver.find_element_by_css_selector('"a[href*=%s]" %id').click()

but i can't click on url

Upvotes: 1

Views: 2167

Answers (1)

alecxe
alecxe

Reputation: 473863

The %id part should be outside of the string for the string formatting to properly work:

driver.find_element_by_css_selector("a[href*=%s]" % id)

Note that this assumes that id has alphanumeric characters only. If not, you need to enclose the value into the quotes:

driver.find_element_by_css_selector('a[href*="%s"]' % id)

Upvotes: 2

Related Questions