python - Selenium - 在脚本中提取 onclick 事件目标 url

标签 python selenium selenium-webdriver web-scraping onclick

假设我有一个包含以下元素的 HTML 页面:

<script>
    function change_url(){
        window.location.href='http://www.google.com/';
    }
</script>

<button type="button" onclick="change_url()">
     Go to Google
</button>

有没有办法在不让浏览器访问URL的情况下获取按钮的目标URL?

谢谢。

最佳答案

根据您的问题标题,Selenium 不可能在不让浏览器访问 URL 的情况下获取按钮的目标 URL 作为 Selenium 模拟启动浏览器实例用户交互

启动浏览器后,要获取目标 URL http://www.google.com/,您可以提取页面源代码并使用 http://www.google.com/>split() 函数按照以下代码块:

driver.get('https://www.your_url.co.in')
page_source = driver.page_source
text_part = page_source.split("window.location.href='")
my_url = text_part[1].split("';")
print(my_url[0])

关于python - Selenium - 在脚本中提取 onclick 事件目标 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48004453/

相关文章:

python - Django: NoReverseMatch at/'myapp' 不是注册的命名空间

从 django-social-auth 导入测试时出现 Python 导入错误

python - 使用 sqlalchemy 进行连接压缩

python - 使用 Selenium 添加新的 CSS

gwt - 如何克服 Selenium 中的 Element id 异常?

python - 使用 Python 和 Selenium 按标题查找并单击元素

python - 效率: Check if value in Pandas DataFrame has changed with an specific threshold

java - Selenium WebDriver - 如何按住鼠标右键?

java - Selenium 网络驱动程序 : Need solution for a very specific issue pertaining to @BeforeTest (TestNG)

java - Selenium TestNG 页面对象模型框架中的问题