Peter Tsung
Peter Tsung

Reputation: 935

how to click the second link using selenium driver in scrapy (using python)

i want to use selenium driver using python to click the second link in the below html. But i can't figure out how to make it.

<div class="outerResults" style="">
    <div class="results searchResults" style="display: block;">

        <table cellspacing="0" id="resultGroup">
            <colgroup>
            </colgroup>
            <tbody>
                <tr>
                    <td class="selector">
                        <a href="#" id="p_-1454220127" title="Gary Smith" class="checkbox unchecked">&nbsp;</a>
                    </td>
                    <td class="personName">
                        <a href="#!search/profile/person?personId=-1454220127&amp;targetid=profile">
                        Gary Smith
                        </a>
                    </td>
                    <td class="title">
                        Chief Executive Officer and Co-Founder
                        <input type="hidden" name="" value="Chief Executive Officer and Co-Founder" id="personTitle">
                        <br>
                        <div class="companyLink">
                            <br>
                            <a href="#!search/profile/company?companyId=104333869&amp;targetid=profile" class="">
                            Tesla Motors Inc
                            </a>
                        </div>
                    </td>
                    <td class="contact">
                        <span class="unmaskContactText">Click to view this profile</span>
                        <span class="detailContactMask">Email</span>
                        <span class="upsellContainer" rel="upsellContactInfo">&nbsp;</span><br>
                        <div class="phoneNumber">
                            <span class="detailContactMask">Phone</span>
                        </div>
                    </td>
                    <td class="date">
                        <span class="date" title="Last Update">
                        5/21/14
                        </span>
                    </td>
                </tr>
                <tr>
                    <td class="selector">
                        <a href="#" id="p_-1832984054" title="Tesla EVs" class="checkbox unchecked">&nbsp;</a>
                    </td>
                    <td class="personName">
                        <a href="#!search/profile/person?personId=-1832984054&amp;targetid=profile">
                        Tesla EVs
                        </a>
                    </td>
                    <td class="title">
                        Chief Executive Officer
                        <input type="hidden" name="" value="Chief Executive Officer" id="personTitle">
                        <br>
                        <div class="companyLink">
                        <br>
                            <a href="#!search/profile/company?companyId=104333869&amp;targetid=profile" class="">
                            Tesla Motors Inc
                            </a>
                        </div>
                    </td>
                    <td class="contact">
                        <span class="unmaskContactText">Click to view this profile</span>
                        <span class="detailContactMask">Email</span>
                        <span class="upsellContainer" rel="upsellContactInfo">&nbsp;</span><br>
                        <div class="phoneNumber">
                            <span class="detailContactMask">Phone</span>
                        </div>
                    </td>
                    <td class="date">
                        <span class="date" title="Last Update">
                        6/24/15
                        </span>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</div>

The aim is to click <td class="personName">'s text such as Gary Smith and Tesla EVs. And i have tried to click Gary Smith using below code

WebDriverWait(self.driver,10).until(EC.presence_of_element_located((By.CLASS_NAME, "personName"))).click()

but just nothing happened. And also i don't know how to use selenium driver to click Tesla EVs

i would like to say that with different input, the content name changed dynamically. So to be precise, i want to click the first and the second link while the inside content name is random

Anyone who could give me a hint would be highly appreciated.

Upvotes: 2

Views: 492

Answers (1)

Andersson
Andersson

Reputation: 52665

To click on Gary Smith link try:

WebDriverWait(self.driver,10).until(EC.presence_of_element_located((By.XPATH, "//a[.='Gary Smith']"))).click()

or

WebDriverWait(self.driver,10).until(EC.presence_of_element_located((By.LINK_TEXT, 'Gary Smith'))).click()

Same for Tesla EVs:

WebDriverWait(self.driver,10).until(EC.presence_of_element_located((By.XPATH, "//a[.='Tesla EVs']"))).click()

or

WebDriverWait(self.driver,10).until(EC.presence_of_element_located((By.LINK_TEXT, "Tesla EVs"))).click()

If the link text is not constant, try to use following selector:

# For 'Gary Smith'
WebDriverWait(self.driver,10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "tr:nth-child(1) > td.personName > a"))).click() 
# For 'Tesla EVs'
WebDriverWait(self.driver,10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "tr:nth-child(2) > td.personName > a"))).click()

Upvotes: 1

Related Questions