Clicking on a link via selenium

2020-02-10 11:03发布

I am trying to do some webscraping via Selenium. My question is very simple: How do you find a link and then how do you click on it? For instance: The following is the HTML that I am trying to web-scrape:

<td bgcolor="#E7EFF9">
  <a href="javascript:selectDodasaDetdasdasy(220011643,'Kdasdası');" target="_self">
   Details
  </a>
</td>

So, as you can see the word "Details" is a link.

How can I find that link using Selenium and click on it?

4条回答
欢心
2楼-- · 2020-02-10 11:50

One thing is missed by everyone. Its a list by the below statement. You need to take select an element from this list.

driver.find_element_by_link_text('Details')

If you check

for i in driver.find_element_by_link_text('Details')
    i.click()

BINGO :-)

查看更多
▲ chillily
3楼-- · 2020-02-10 11:53

You can try to click link by using xpath locator e.g.

link=driver.find_element_by_xpath(.//*[@id="content"]/div[3]/div/div/div[2]/h4)

link.click()
查看更多
闹够了就滚
4楼-- · 2020-02-10 12:06

Then you can try something like this.

for (int i=0; i&lttd.length(); i++){
        driver.find_element_by_xpath("(//a[contains(text(),'Details')])[i]").click()
        }
查看更多
我想做一个坏孩纸
5楼-- · 2020-02-10 12:07

You can use find_element_by_link_text:

For example:

link = driver.find_element_by_link_text('Details')

To Click on it, just call click method:

link.click()
查看更多
登录 后发表回答