Selenium 获取对象 url

发布于 2025-01-12 00:46:44 字数 947 浏览 0 评论 0原文

在selenium python代码中,我可以单击WebElement(例如post_raw.click()

我可以借助selenium方法识别WebElement链接(将被单击)吗?

我知道关于 driver.current_url 但我在点击之前查找链接。我正在查看文档,但没有找到解决方案 https://www.selenium.dev/selenium/docs/api/py/webdriver_remote/selenium.webdriver.remote.webelement.html

我的代码示例:

from selenium import webdriver

# login to facebook code
driver.get("https://touch.facebook.com/dota2")

posts_raw = driver.find_elements_by_xpath("//article")
post_raw = posts_raw[0]
print(type(post_raw)) # <class 'selenium.webdriver.remote.webelement.WebElement'>

post_raw.click() # how can I get post_raw link (which was clicked in this line)

我想要这样的功能:

def get_url_from_WebElement(web_elem: WebElement) -> str:

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 https://www.selenium.dev/selenium/docs/api/py/webdriver_remote/selenium.webdriver.remote.webelement.html

My code example:

from selenium import webdriver

# login to facebook code
driver.get("https://touch.facebook.com/dota2")

posts_raw = driver.find_elements_by_xpath("//article")
post_raw = posts_raw[0]
print(type(post_raw)) # <class 'selenium.webdriver.remote.webelement.WebElement'>

post_raw.click() # how can I get post_raw link (which was clicked in this line)

I want function like this:

def get_url_from_WebElement(web_elem: WebElement) -> str:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雪若未夕 2025-01-19 00:46:44

您可以尝试从 xpath 获取 href。

问题

示例

我想获取“问题”形式的链接,因此我将 .get_attribute("href") 添加到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")

如果我们打印它,我们会得到:

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

You can try get href from xpath.

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
月光色 2025-01-19 00:46:44

假设我有一个 webelement,我想使用该 webelement 来获取 a 标签。这不会点击通常位于 web_element 中间的元素。

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

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 ''
蝶…霜飞 2025-01-19 00:46:44

您的意思是:

def get_url_from_webelement(xpath):
    the_url = driver.find_element(By.XPATH, "\"" + xpath + "\"").get_attribute("href")
    return the_url

Do you mean:

def get_url_from_webelement(xpath):
    the_url = driver.find_element(By.XPATH, "\"" + xpath + "\"").get_attribute("href")
    return the_url
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文