Leila K.
Leila K.

Reputation: 77

Python- Getting link of new webpage using selenium

I'm new to selenium and I wrote this code that gets user input and searches in ebay but I want to save the new link of the search so I can pass it on to BeautifulSoup.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

search_ = input()
browser = webdriver.Chrome(r'C:\Users\Leila\Downloads\chromedriver_win32')
browser.get("https://www.ebay.com.au/sch/i.html?_from=R40&_trksid=p2499334.m570.l1311.R1.TR12.TRC2.A0.H0.Xphones.TRS0&_nkw=phones&_sacat=0")
Search = browser.find_element_by_id('kw')
Search.send_keys(search_)
Search.send_keys(Keys.ENTER)
#how do you write a code that gets the link of the new page it loads

Upvotes: 2

Views: 44

Answers (1)

AzyCrw4282
AzyCrw4282

Reputation: 7744

To extract a link from a webpage, you need to make use of the HREF attribute and use the get_attribute() method.

This example from here illustrates how it would work.

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
driver = webdriver.Chrome(chrome_options=options)

driver.get('https://www.w3.org/')
for a in driver.find_elements_by_xpath('.//a'):
    print(a.get_attribute('href'))

In your case, do:

Search = browser.find_element_by_id('kw')
page_link = Search.get_attribute('href')

Upvotes: 2

Related Questions