如何使用 Control + Click of Selenium Webdriver 在同一窗口的新选项卡中的主选项卡中打开嵌入在 webelement 中的链接 [英] How to open a link embeded in a webelement with in the main tab, in a new tab of the same window using Control + Click of Selenium Webdriver

查看:36
本文介绍了如何使用 Control + Click of Selenium Webdriver 在同一窗口的新选项卡中的主选项卡中打开嵌入在 webelement 中的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在主选项卡的 web 元素中嵌入了一个链接,我想使用 Selenium Webdriver 和 python 在同一窗口的新选项卡中打开该链接.在新选项卡中执行一些任务,然后关闭该选项卡并返回主选项卡.我们将通过右键单击链接手动执行此操作,然后选择在新选项卡中打开"以在新选项卡中打开该链接.

我是 Selenium 的新手.我正在使用 Selenium 和 BeautifulSoup 进行网页抓取.我只知道如何点击链接.我已经阅读了很多帖子,但找不到正确的答案

url = "https://www.website.com"路径 = r'path_to_chrome_driver'驱动器 = webdriver.Chrome(路径)drive.implicitly_wait(30)drive.get(url)来源=drie.page_sourcepy_button = driver.find_element_by_css_selector("div[data-res-position = '1']")py_button.click()

我希望 div[data-res-position = '1'] 中的链接在新标签页中打开

解决方案

由于在 Parent Tab 的 webelement 中嵌入了一个链接,可以打开 link在同一窗口的新标签中使用

<块引用>

您可以在在 Selenium Webdriver 中使用 Ctrl + 单击组合打开一个新选项卡

<小时>

更新

要将 Selenium 的重点转移到新打开的选项卡上,您可以在 在新标签页 Selenium + Python 中打开网页

There is a link embedded in a web element in the Main Tab, I want to open that link in a new tab in the same window using Selenium Webdriver and python. Perform some tasks in the new tab and then close that tab and return back to the main tab. Manually we will do this by right-clicking on the link and then select "open in new tab" to open that link in new tab.

I am new to Selenium. I am using Selenium and BeautifulSoup to web scrape. I only know how to click on a link. I have read through many posts but couldn't find a proper answer

url = "https://www.website.com"
path = r'path_to_chrome_driver'
drive = webdriver.Chrome(path)
drive.implicitly_wait(30)
drive.get(url)
source = drie.page_source

py_button = driver.find_element_by_css_selector("div[data-res-position = '1']")
py_button.click()

I expect the link in div[data-res-position = '1'] to open in a new tab

解决方案

As there is a link embedded within in the webelement in the Parent Tab, to open the link in a New Tab in the same window using Selenium and Python you can use the following solution:

To demonstrate the workflow the url https://www.google.com/ was opened in the Parent Tab and then open in new tab functionalty is implemented through ActionChains methods key_down(), click() and key_up() methods.

  • Code Block:

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.action_chains import ActionChains
    from selenium.webdriver.common.keys import Keys
    import time
    
    options = webdriver.ChromeOptions() 
    options.add_argument("start-maximized")
    driver = webdriver.Chrome(options=options, executable_path=r'C:WebDriverschromedriver.exe')
    driver.get("https://www.google.com/")
    link = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Gmail")))
    ActionChains(driver).key_down(Keys.CONTROL).click(link).key_up(Keys.CONTROL).perform()
    

  • Note: You need to replace (By.LINK_TEXT, "Gmail") with your desired locator e.g. ("div[data-res-position = '1']")

  • Browser Snapshot:

You can find a relevant Java based solution in Opening a new tab using Ctrl + click combination in Selenium Webdriver


Update

To shift Selenium's focus to the newly opened tab you can find a detailed discussion in Open web in new tab Selenium + Python

这篇关于如何使用 Control + Click of Selenium Webdriver 在同一窗口的新选项卡中的主选项卡中打开嵌入在 webelement 中的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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