Home > database >  Python - Copy link of currently used page
Python - Copy link of currently used page

Time:01-11

I am using Selenium on Python. With this short example below, I switch from the page enter image description here

Is it possible to copy this link (google.com/search?q...) with Python, by assigning a variable name to the link?

Below, the code used to arrive to this page

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


link = 'https://www.google.com/'
driver = webdriver.Chrome('C:/Users/User/Downloads/chromedriver.exe')
driver.get(link)
time.sleep(2)
driver.maximize_window()
print(driver.page_source)


driver.find_element_by_xpath("//div[text()='Accept']").click() # Accept Cookies
driver.find_element_by_css_selector('input[]').click()
driver.find_element_by_css_selector('input[]').send_keys('some text')
driver.find_element_by_css_selector('input[]').send_keys(Keys.RETURN)

Thanks in advance!

CodePudding user response:

Current URL is available as driver.current_url so just add

url = driver.current_url
print(url)

immediately after last line

  •  Tags:  
  • Related