简体   繁体   English

如何使用python和selenium IDE获取网页上的所有链接

[英]How to get all links on a web page using python and selenium IDE

I want to get all link from a web page using selenium ide and python. 我想使用硒ide和python从网页获取所有链接。

For example if I search test or anything on google website and I want all link related to that. 例如,如果我在Google网站上搜索测试或任何内容,并且希望所有与之相关的链接。

Here is my code 这是我的代码

 from selenium import webdriver
from selenium.webdriver.common.keys import Keys
baseurl="https://www.google.co.in/?gws_rd=ssl"
driver = webdriver.Firefox()
driver.get(baseurl)
driver.find_element_by_id("lst-ib").click()
driver.find_element_by_id("lst-ib").clear()
driver.find_element_by_id("lst-ib").send_keys("test") 
link_name=driver.find_element_by_xpath(".//*[@id='rso']/div[2]/li[2]/div/h3/a")
print link_name
driver.close()

Output 产量

 <selenium.webdriver.remote.webelement.WebElement object at 0x7f0ba50c2090>

Using xpath $x(".//*[@id='rso']/div[2]/li[2]/div/h3/a") in Firebug's console. 在Firebug的控制台中使用xpath $x(".//*[@id='rso']/div[2]/li[2]/div/h3/a")

Output [a jtypes2.asp] 输出 [一个jtypes2.asp]

How can I get links content from a object. 如何从对象获取链接内容。

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
baseurl="https://www.google.co.in/?gws_rd=ssl"
driver = webdriver.Firefox()
driver.get(baseurl)
driver.find_element_by_id("lst-ib").click()
driver.find_element_by_id("lst-ib").clear()
driver.find_element_by_id("lst-ib").send_keys("test")
driver.find_element_by_id("lst-ib").send_keys(Keys.RETURN)
driver.implicitly_wait(2)
link_name=driver.find_elements_by_xpath(".//*[@id='rso']/div/li/div/h3/a")
for link in link_name:
    print link.get_attribute('href')

Try the above code. 试试上面的代码。 Your code doesn't send a RETURN key after giving the search keyword. 输入搜索关键字后,您的代码不会发送RETURN键。 Also I've made changes to implicitly wait for 2 seconds to load the search results and I've changed xpath to get all links. 另外,我还进行了更改,以隐式等待2秒钟以加载搜索结果,并且更改了xpath以获取所有链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当使用 Python 和 Selenium 抓取 web 时,如何从单个页面获取所有 href 链接? - How can I get all the href links from a single page when web scraping using Python and Selenium? 如何使用Selenium IDE和python从pdf pag(链接)获取所有页面文本 - How to get all pages text from pdf pag(links) using selenium IDE and python 使用 Python Selenium 4.1.0 获取页面上的所有链接 - get all links on a page with Python Selenium 4.1.0 如何使用Selenium Web驱动程序python单击网页中目录列表的不同链接 - how to click on different links of directory listing in a web page using selenium web-driver python 如何使用python和mechanize从php页面获取所有链接 - How to get all links from php page using python and mechanize 如何使用 selenium 单击 web 页面中的所有链接 - How can I click all the links in a web page with selenium 如何在 Python 中使用 Selenium 从网页中获取数据? - How to get data from a web-page using Selenium in Python? 如何获取 Python Scrapy 以从 web 页面中提取所有外部链接的所有域? - How to I get Python Scrapy to extract all of the domains of all external links from a web page? 使用python从网页中提取所有链接 - Extract all links from a web page using python Python + Web抓取+ scrapy:如何从IMDb页面获取所有电影的链接? - Python + web scraping + scrapy : How to get the links to all movies from an IMDb page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM