Home > Back-end >  Selenium get object url
Selenium get object url

Time:03-07

In selenium python code i can click to WebElement (e.g post_raw.click())

Can I identify WebElement link (which will be clicked) with help of selenium methods?

I know about driver.current_url but I am lookink for link before click. I was looking in documentation, but don't find solution Question

Example :

I want to get link form "question" so i add .get_attribute("href") to find_element

question_link = driver.find_element(By.XPATH, "/html/body/div[3]/div[2]/div/div[1]/div[3]/div[3]/h2[1]/a[1]").get_attribute("href")

if we print it we get :

https://stackoverflow.com/questions/71371250/selenium-get-object-url

CodePudding user response:

Let's say I have a webelement and I want to use that webelement to get the a tag. This does not get the element clicked which is just the middle of the web_element generally.

def get_url_from_WebElement(web_element):
    try:
        elem=web_element.find_element(BY.XPATH,".//a").get_attribute("href")
        return elem
    except:
        return ''
  • Related