[英]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.