Python selenium click tab

josecolella / python-selenium-open-tab.md

On a recent project, I ran into an issue with Python Selenium webdriver. There’s no easy way to open a new tab, grab whatever you need and return to original window opener.

Here’s a couple people who ran into the same complication:

So, after many minutes (read about an hour) of searching, I decided to do find a quick solution to this problem.

First thing, I’ve broken down all the steps that were required to do by my program:

  1. Open a new window/tab by simulating a click on a link
  2. Add focus on this new tab
  3. Wait for an element on the new page to be rendered (ui.WebDriverWait)
  4. Do whatever I have to do on this new page
  5. Close the tab
  6. Return focus on original window opener
import selenium.webdriver as webdriver import selenium.webdriver.support.ui as ui from selenium.webdriver.common.keys import Keys from time import sleep 
browser = webdriver.Firefox() browser.get('https://www.google.com?q=python#q=python') first_result = ui.WebDriverWait(browser, 15).until(lambda browser: browser.find_element_by_class_name('rc')) first_link = first_result.find_element_by_tag_name('a') # Save the window opener (current window, do not mistaken with tab. not the same) main_window = browser.current_window_handle # Open the link in a new tab by sending key strokes on the element # Use: Keys.CONTROL + Keys.SHIFT + Keys.RETURN to open tab on top of the stack first_link.send_keys(Keys.CONTROL + Keys.RETURN) # Switch tab to the new tab, which we will assume is the next one on the right browser.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.TAB) # Put focus on current window which will, in fact, put focus on the current visible tab browser.switch_to_window(main_window) # do whatever you have to do on this page, we will just got to sleep for now sleep(2) # Close current tab browser.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 'w') # Put focus on current window which will be the window opener browser.switch_to_window(main_window)

Источник

Читайте также:  All html escape characters

How To Switch Tabs In A Browser Using Selenium Python?

Join

This article is a part of our Content Hub. For more in-depth resources, check out our content hub on Selenium Python Tutorial.

Selenium automation offers dexterous ways to perform day-to-day tasks most efficiently. From capturing screenshots to testing PDF files, there’s no limit to what you can do with Selenium automation. Developers and testers are masters of drilling websites and finding loopholes. More often than not, this drill involves switching tabs multiple times a day. To reduce the manual effort that goes into doing so, we recommend using Python Selenium to switch tabs.

In this article, we will help you master multiple ways to switch tabs in Selenium using Python. Let’s get started –

TABLE OF CONTENT

When Do We Need To Open Or Switch Tabs In Selenium Automation?

Opening a new tab or switching between multiple tabs is one of the most basic functions performed by all of us every single day. Let us look into some scenarios when opening or switching tabs comes in handy during Selenium automation.

App Suits – Testing Features That Can Work Parallely

The most obvious scenario is when your application is too big and has mini-applications within it. For example, if you have used Linkedin, you might have come across ‘Linkedin Learning’ & ‘Linkedin Sales Navigator’ as two sub-applications. Similarly, many other applications are composed of several other smaller constituent applications. For example, G-suite, Youtube (media player & creator studio), Hubspot, Azure & Amazon, etc., some of these, by default, open up in a new tab. For others, it makes sense to intentionally open a new application in a new tab. This is where Selenium automation testing comes in.

Testing Ecommerce App Features

Let’s take another example of e-commerce application testing. Suppose you are testing an e-commerce website and you want to add a product to the cart. Then, you want to check if it gets reflected on the cart page. Next, you want to add another product to the cart. And then you want to confirm if it is visible in the cart. Doing this in the same browser window and tab could be a tenacious & time-taking task, and that’s where Selenium automation comes in.

Multimedia Uploading Functionality Testing

Another common example could be of uploading multiple videos parallelly to a vlogging platform like Vimeo or Youtube. This is an ideal case for multi-browser, cross-tab testing.

In short, automation testing involves switching tabs in the following scenarios –

  • Testing parallel features of large applications. E.g., Zoho, G-suite, Office365, etc.
  • Testing links that automatically open in a new tab. E.g. – eCommerce applications
  • Parallel testing of independent applications if the need arises.

In this Python Selenium switch tabs tutorial, we will be focusing on opening and switching between the tabs using Python & Selenium. We have dedicated articles for on-page testing operations like handling alerts, drag and drop objects using a mouse, or taking full-page screenshots. You may want to refer to those to dive deeper into Selenium automation use cases.

This certification is for professionals looking to develop advanced, hands-on expertise in Selenium automation testing with Python and take their career to the next level.

Here’s a short glimpse of the Selenium Python 101 certification from LambdaTest:

How To Create A New Tab In Selenium Python?

In order to get started with Python Selenium switch tabs automation, your system needs to be equipped with the required tools. Here are all the prerequisites:

  • You have the latest Selenium-Python bindings, Python3, pip, virtualenv installed in your system
  • You also have a path to the executable WebDriver of your browser choice in your system.

If not, you can simply follow the instructions given in this Selenium 4 with Python blog to get started with Python Selenium switch tab automation. We can open new tabs in Selenium-Python by executing JavaScript as well as by using Selenium’s ActionChains class.

Let’s see how this can be done.

Open New Tabs In Selenium By Executing JavaScript

In the following code, replace “chrome_webdriver_executable_path” with the path to your chrome executable. Ideally, you should add it in your systems PATH variable.

Sample Code 1-

Источник

lrhache / python-selenium-open-tab.md

On a recent project, I ran into an issue with Python Selenium webdriver. There’s no easy way to open a new tab, grab whatever you need and return to original window opener.

Here’s a couple people who ran into the same complication:

So, after many minutes (read about an hour) of searching, I decided to do find a quick solution to this problem.

First thing, I’ve broken down all the steps that were required to do by my program:

  1. Open a new window/tab by simulating a click on a link
  2. Add focus on this new tab
  3. Wait for an element on the new page to be rendered (ui.WebDriverWait)
  4. Do whatever I have to do on this new page
  5. Close the tab
  6. Return focus on original window opener
import selenium.webdriver as webdriver import selenium.webdriver.support.ui as ui from selenium.webdriver.common.keys import Keys from time import sleep 
browser = webdriver.Firefox() browser.get('https://www.google.com?q=python#q=python') first_result = ui.WebDriverWait(browser, 15).until(lambda browser: browser.find_element_by_class_name('rc')) first_link = first_result.find_element_by_tag_name('a') # Save the window opener (current window, do not mistaken with tab. not the same) main_window = browser.current_window_handle # Open the link in a new tab by sending key strokes on the element # Use: Keys.CONTROL + Keys.SHIFT + Keys.RETURN to open tab on top of the stack first_link.send_keys(Keys.CONTROL + Keys.RETURN) # Switch tab to the new tab, which we will assume is the next one on the right browser.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.TAB) # Put focus on current window which will, in fact, put focus on the current visible tab browser.switch_to_window(main_window) # do whatever you have to do on this page, we will just got to sleep for now sleep(2) # Close current tab browser.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 'w') # Put focus on current window which will be the window opener browser.switch_to_window(main_window)

Источник

Open and Close Tabs in a Browser Using Selenium Python

Open and Close Tabs in a Browser Using Selenium Python

  1. Install Selenium and Chrome WebDriver
  2. Open a Tab in a Browser Using Selenium Python
  3. Open a New Tab in a Browser Using Selenium Python
  4. Close a Tab in a Browser Using Selenium Python
  5. Close a Tab and Switch to Another Tab in a Browser Using Selenium Python

Selenium is powerful web automation and testing tool. We write scripts using Selenium, which takes control over web browsers and performs specific actions.

In this guide, we will write a script in Python that will automatically open and close a website in a new tab.

Install Selenium and Chrome WebDriver

To install Selenium, we use the following command.

#Python 3.x pip install selenium 

ChromeDriver is another executable that Selenium WebDriver uses to interact with Chrome. If we want to automate tasks on the Chrome web browser, we also need to install ChromeDriver.

  1. Click on this link. Download Chrome driver according to the version of your Chrome browser and the type of operating system.
  2. If you want to find the version of your Chrome browser, click on the three dots on the top right corner of Chrome, click on Help, and select About Google Chrome. You can see the Chrome version in the about section.
  3. Extract the zip file and run the Chrome driver.

Open a Tab in a Browser Using Selenium Python

We created the WebDriver instance in the following code and specified the path to the Chrome driver. Then, we have set the URL of the target website using the get() method with the driver instance.

It will open the target website in the Chrome browser.

#Python 3.x from selenium import webdriver driver = webdriver.Chrome(r"E:\download\chromedriver.exe") driver.get("https://www.verywellmind.com/what-is-personality-testing-2795420") 

Selenium Open Tab Python

Open a New Tab in a Browser Using Selenium Python

To open a new tab in the same browser window, we will use the JavaScript executer. It executes JavaScript commands using the execute_script() method.

We will pass the JavaScript command to this method as an argument. We will use the window.open() command to open another tab in the window.

The window handle stores the unique address of the windows opened in the web browser. The switch_to_window() method switches to the specified window address.

1 represents the address of the second window. Finally, we will provide the URL of the new website using the get() method.

#Python 3.x from selenium import webdriver driver = webdriver.Chrome(r"E:\download\chromedriver.exe") driver.get("https://www.verywellmind.com/what-is-personality-testing-2795420") driver.execute_script("window.open('');") driver.switch_to.window(driver.window_handles[1]) driver.get('https://www.indeed.com/career-advice/career-development/types-of-personality-test') 

Selenium Open New Tab Python

Close a Tab in a Browser Using Selenium Python

We will use the close() method with the driver to close the tab.

#Python 3.x from selenium import webdriver driver = webdriver.Chrome(r"E:\download\chromedriver.exe") url = "https://www.16personalities.com/free-personality-test" driver.get(url) driver.close() 

Close a Tab and Switch to Another Tab in a Browser Using Selenium Python

Using Selenium in the following code, we have opened a URL in a tab. We opened another tab and switched to it using the switch_to.window(driver.window_handles[1]) .

The new tab will open the specified URL. Now, we will close this tab using the close() method and switch back to the previous tab using the switch_to.window(driver.window_handles[0]) method.

#Python 3.x from selenium import webdriver driver = webdriver.Chrome(r"E:\download\chromedriver.exe") url = "https://www.16personalities.com/free-personality-test" driver.get(url) driver.execute_script("window.open('');") driver.switch_to.window(driver.window_handles[1]) driver.get("https://www.16personalities.com/personality-types") driver.close() driver.switch_to.window(driver.window_handles[0]) 

Selenium Switch New Tab

Selenium Close New Tab and Switch to Previous Tab

I am Fariba Laiq from Pakistan. An android app developer, technical content writer, and coding instructor. Writing has always been one of my passions. I love to learn, implement and convey my knowledge to others.

Related Article — Python Selenium

Источник

Оцените статью