selenium 获取链接
时间: 2023-04-12 13:00:45 浏览: 75
可以使用以下代码获取链接:
```python
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.example.com")
link = driver.find_element_by_link_text("Link Text").get_attribute("href")
print(link)
```
其中,`webdriver.Chrome()` 是使用 Chrome 浏览器驱动,`driver.get("https://www.example.com")` 是打开链接,`driver.find_element_by_link_text("Link Text")` 是通过链接文本找到链接元素,`get_attribute("href")` 是获取链接地址。
相关问题
selenium获取页面的href链接
对于selenium获取页面的href链接,我可以给出以下建议:首先,使用selenium定位到相应的元素,然后使用get_attribute('href')方法获取该元素的href属性值。需要注意的是,有些页面中的链接可能是通过JS动态生成的,这种情况下需要使用selenium的execute_script方法执行相应的JS代码,以获取页面链接。
selenium获取href里的链接
可以使用Selenium的`get_attribute()`方法来获取元素的属性值,例如获取`<a>`标签的`href`属性值,可以使用以下代码:
```python
from selenium import webdriver
# 创建浏览器对象
browser = webdriver.Chrome()
# 打开网页
browser.get('https://www.example.com')
# 获取第一个<a>标签的href属性值
link = browser.find_element_by_tag_name('a').get_attribute('href')
# 输出链接
print(link)
# 关闭浏览器
browser.quit()
```
以上代码会打开一个Chrome浏览器,访问`https://www.example.com`网页,然后获取第一个`<a>`标签的`href`属性值,并输出到控制台。你可以根据自己的需求修改代码,获取其他元素的属性值。