Do you operate the same browser every day?
Let's leave such browser operation to Selenium!
Would you like to Ctrl + click a link to open it in a new tab while touching your browser? I do very well w
It will be such a series of flow.
sample.py
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
import platform
driver = webdriver.Chrome("chromedriver.exe") #chromedriver.exe load
#
#
#Processing up to the corresponding page
#
#
#Element to click
element = driver.find_element_by_xpath('/html/body/hogehoge')
#element = driver.find_element_by_link_text("hogehoge") #Click here if you want to get it with link text
#Handle list before clicking
handles_befor = driver.window_handles
#(Link)Open element in new tab
actions = ActionChains(driver)
if platform.system() == 'Darwin':
#Command key because it is a Mac
actions.key_down(Keys.COMMAND)
else:
#Control key because it is not a Mac
actions.key_down(Keys.CONTROL)
actions.click(element)
actions.perform()
#Wait up to 30 seconds for new tabs to open
WebDriverWait(driver, 30).until(lambda a: len(driver.window_handles) > len(handles_befor))
#Handle list after clicking
handles_after = driver.window_handles
#Handle list diff
handle_new = list(handles_after - sethandles_befor)
#Go to new tab
driver.switch_to.window(handle_new[0])
Be careful not to overload
Let's put wait
and sleep
as appropriate.
sample.py
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
Load the Selenium library and the packages you need this time.
sample.py
import platform
Load the library to determine the OS.
sample.py
driver = webdriver.Chrome("chromedriver.exe") #chromedriver.exe load
#
#
#Processing up to the corresponding page
#
#
Let's start the chrome driver and operate it.
sample.py
#Element to click
element = driver.find_element_by_xpath('/html/body/hogehoge')
#element = driver.find_element_by_link_text("hogehoge") #Click here if you want to get it with link text
Assign the element you click with xpath
or link_text
to a variable.
sample.py
#Handle list before clicking
handles_befor = driver.window_handles
Gets the handle in list format before the new tab opens.
sample.py
#(Link)Open element in new tab
actions = ActionChains(driver)
if platform.system() == 'Darwin':
#Command key because it is a Mac
actions.key_down(Keys.COMMAND)
else:
#Control key because it is not a Mac
actions.key_down(Keys.CONTROL)
actions.click(element)
actions.perform()
You can use a class called ʻActionChains` to record and execute keystrokes. On Mac and other operating systems, the shortcut key that opens in a new tab is different for the Google Chrome browser.
Mac: command + click Windows and Linux: Ctrl + click
The OS is judged by the platform library and conditional branching is performed.
ʻThe key operation recorded by actions.perform () `is executed.
sample.py
#Wait up to 30 seconds for new tabs to open
WebDriverWait(driver, 30).until(lambda a: len(driver.window_handles) > len(handles_befor))
It may take some time when a new tab opens, so let it wait.
WebDriverWait (driver, 30)). until
will allow the driver to wait up to 30 seconds.
In lambda a: len (driver.window_handles)> len (handles_befor)
, it is described in one line using a lambda expression.
len (handles_befor)
than the number of handles before opening in a new tab
len (driver.window_handles)
The wait will be canceled when the number of handles exceeds the number of handles at the moment when a new tab is opened.
sample.py
#Handle list after clicking
handles_after = driver.window_handles
Gets the handle in list format after the new tab opens.
sample.py
#Handle list diff
handle_new = list(handles_after - handles_befor)
You can get the difference by subtracting the list with -
.
I'm assigning it to a variable.
sample.py
#Go to new tab
driver.switch_to.window(handle_new[0])
The difference handle_new [0]
becomes the handle of the new tab, so move it with switch_to.window
.
So that you can always go back if you open too many tabs and don't understand why (Let's not do that w)
It may be good to get the current handle in advance.
sample.py
#Current window(tab)Substitute the handle of
current_handle = driver.current_window_handle
#
#
#processing
#
#
#Current window(tab)Move to
driver.switch_to.window(current_handle)
Thank you very much!
Recommended Posts