如何使用Python Selenium ChromeDriver在新标签页中打开链接 [英] How to open a link in a new tab with Python selenium ChromeDriver

查看:1080
本文介绍了如何使用Python Selenium ChromeDriver在新标签页中打开链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请提供有关如何使用Python硒Chrome浏览器Webdriver在新标签中打开链接的建议.因此,我既不问如何简单地打开一个新标签,也不问问如何用Firefox打开一个新标签.

Please, advise on how to open a link in a new tab with Python selenium Chrome Webdriver. So, I am not asking how to simply open a new tab, nor on how to open a new tab with Firefox.

此页面在新标签页中打开同一页面:

This one opens the same page in a new tab:

first_link.send_keys(Keys.CONTROL + Keys.ENTER)

这也是:

first_link.send_keys(Keys.CONTROL + Keys.ENTER)

alecxe对的回答不起作用,因为我无法获得链接所指向的网址.我只需要模拟鼠标右键单击>在新选项卡中打开链接".

alecxe answer to this question does not work since, I cannot get an url a link is pointing at. I need to simply emulate Right-mouse-click > "Open link in a new tab".

问题出在网站 https://www.pagesjaunes.fr 本身.由于某种原因,它在搜索结果中为其条目输入了错误的网址.例如元素:

The problem turns out to be with the website https://www.pagesjaunes.fr itself. For some reason it give wrong urls for its entries in search results. For instance for element:

//*[@id="bi-bloc-0437413413085060110003C0001"]/div[2]/header/div[1]/div/h2/a[2]

因此,当尝试通过Selenium或请求访问此元素的链接时,它将重定向到搜索结果本身的页面.有鉴于此,我决定采用其他方法来解决此问题.

Therefore, when the link of this element is attempted to be accessed via Selenium or requests, it redirects to the page of the search results itself. In light of this I decided to use a different approach to solving the issue.

因此,我决定放弃在新标签页中打开链接的方法.

Therefore I decided to abandon the approach of opening the link in a new tab.

推荐答案

尝试一下:

driver.find_element_by_tag_name("body").send_keys(Keys.COMMAND + 't')

这篇关于如何使用Python Selenium ChromeDriver在新标签页中打开链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆