Mukesh
Mukesh

Reputation: 431

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.

For example if I search test or anything on google website and I want all link related to that.

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.

Output [a jtypes2.asp]

How can I get links content from a object.

Upvotes: 1

Views: 2139

Answers (1)

thavan
thavan

Reputation: 2429

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. 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.

Upvotes: 1

Related Questions