Selenium 获取对象 url
在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以尝试从 xpath 获取 href。
示例:
我想获取“问题”形式的链接,因此我将
.get_attribute("href")
添加到find_element
如果我们打印它,我们会得到:
You can try get href from xpath.
Example :
I want to get link form "question" so i add
.get_attribute("href")
tofind_element
if we print it we get :
假设我有一个 webelement,我想使用该 webelement 来获取 a 标签。这不会点击通常位于 web_element 中间的元素。
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.
您的意思是:
Do you mean: