Python 通过硒单击链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18597735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Clicking on a link via selenium
提问by canbaran
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:
我正在尝试通过 Selenium 进行一些网页抓取。我的问题很简单:你如何找到一个链接,然后你如何点击它?例如:以下是我尝试抓取的 HTML:
<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?
如何使用 Selenium 找到该链接并单击它?
采纳答案by falsetru
You can use find_element_by_link_text
:
您可以使用find_element_by_link_text
:
For example:
例如:
link = driver.find_element_by_link_text('Details')
To Click on it, just call click method:
要点击它,只需调用 click 方法:
link.click()
回答by Paras
Then you can try something like this.
然后你可以尝试这样的事情。
for (int i=0; i<td.length(); i++){
driver.find_element_by_xpath("(//a[contains(text(),'Details')])[i]").click()
}
回答by Gayatri
You can try to click link by using xpath locator e.g.
您可以尝试使用 xpath 定位器单击链接,例如
link=driver.find_element_by_xpath(.//*[@id="content"]/div[3]/div/div/div[2]/h4)
link.click()
回答by Mr. Bordoloi
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 :-)
答对了 :-)