简体   繁体   English

如何使用python和selenium在新选项卡中打开链接

[英]How to open a link in a new tab with python and selenium

I want to open links that I find on a website in a new tab. 我想在新标签页面中打开我在网站上找到的链接。 I have tried to open a new tab and pass the url of the link to the driver as suggested here , however, the new tab simply will not open. 我试图打开一个新选项卡,并按照此处的建议将链接的URL传递给驱动程序,但是,新选项卡根本无法打开。 (There are a couple of other suggestion for how to open a new tab, but none of them seem to work for me.) (关于如何打开新标签还有其他一些建议,但它们似乎都不适用于我。)

So my latest attempt was to right-click the link and press "t" to open the link in a new tab, like so: 所以我最近的尝试是右键单击链接并按“t”在新选项卡中打开链接,如下所示:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains

# Using Firefox to access web
driver = webdriver.Firefox()

# Open the website
driver.get('https://registers.esma.europa.eu/publication/searchRegister?core=esma_registers_firds')

# search for information
elem = driver.find_element_by_id('keywordField')
elem.clear()
elem.send_keys('XS1114155283')

button = driver.find_element_by_id('searchSolrButton')
button.click()

table_body = driver.find_element_by_xpath("//table[@id='T01']/tbody")
for link in table_body.find_elements_by_tag_name('a'):

    act = ActionChains(driver)
    act.context_click(link)
    act.send_keys("t")
    act.perform()

    # ... do something in the new tab, close tab, and open next link ...

However, I get an error message on act.perform() which reads 但是,我在act.perform()上收到一条错误消息

MoveTargetOutOfBoundsException: (974, 695) is out of bounds of viewport width (1366) and height (654)

I managed a work around by opening the link in a new window, but I would really prefer the tab-version, as it would take longer to open a new browser window rather than a new tab. 我通过在新窗口中打开链接来管理工作,但我更喜欢tab-version,因为打开新的浏览器窗口而不是新选项卡需要更长的时间。

You can use driver.execute_script() function to open link in a new tab 您可以使用driver.execute_script()函数在新选项卡中打开链接

from selenium import webdriver

# Using Firefox to access web

driver = webdriver.Firefox()

# Open the website
driver.get('https://registers.esma.europa.eu/publication/searchRegister?core=esma_registers_firds')

# search for information
elem = driver.find_element_by_id('keywordField')
elem.clear()
elem.send_keys('XS1114155283')

button = driver.find_element_by_id('searchSolrButton')
button.click()

table_body = driver.find_element_by_xpath("//table[@id='T01']/tbody")
for link in table_body.find_elements_by_tag_name('a'):
    href = link.get_attribute('href')
    # open in new tab
    driver.execute_script("window.open('%s', '_blank')" % href)
    # Switch to new tab
    driver.switch_to.window(driver.window_handles[-1])

    # Continuous your code

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Python selenium ChromeDriver 在新标签页中打开链接 - How to open a link in a new tab with Python selenium ChromeDriver python selenium 在新标签页中打开链接并继续 session 在新标签页中 - python selenium open link in new tab and continue session in new tab 如何使用 selenium 和 python “在新选项卡中打开”? - How to “open in a new tab” with selenium and python? 如何使用Selenium和Python在新选项卡中打开新链接(单击网页中的元素后生成)? - How to open the new link (generated after clicking an element in a web page) in a new tab using Selenium and Python? Python - 使用 Selenium WebDriver 在新的 Chrome 选项卡中打开链接? - Python - Open a link in new Chrome tab with Selenium WebDriver? python selenium,在新标签页中打开链接而不导航到它 - python selenium, open link in new tab without navigating to it 在Python中使用Selenium WebDriver在新标签页/窗口中打开链接 - Using Selenium WebDriver in Python to Open Link in New Tab/Window 如何通过使用 python selenium 单击链接按钮访问在新选项卡中打开的元素? - How to access an element that open in new tab via clicking a link button using python selenium? 如何使用 python selenium 在新选项卡中打开链接而不知道其 URL - How to open a link without knowing its URL in new tab using python selenium 如何单击链接并在新标签的geckodriver selenium中打开它? - How to click a link and open it in a new tab geckodriver selenium?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM