[英]New tab python selenium
我对包含 Selenium 的代码有疑问:当我在新选项卡中打开链接时,程序没有在新选项卡上运行,而是在旧选项卡上运行。 我尝试使用CTRL + TAB更改 Tab 但这不起作用。 然后我尝试了以下代码,我在另一个问题中发现: Opening link in the new tab and switch between tabs (Selenium WebDriver + Python)
这也没有用......有人知道吗?
PS:我正在使用 Python 3.8.3
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import ui
import time
username = "xxxxxxxxx"
password = "XXXXXXXXX"
Kunde = "XXXXXXXXXXXX"
PATH = "C:\Drivers\Webdriver\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://areaclienti.gse.it/")
print(driver.title)
#some navigation in between
#some navigation in between
#some navigation in between
#some navigation in between
first_link = driver.find_element_by_xpath("/html/body/app-root/app-private/nb-layout/div[1]/div/div/div/div/nb-layout-column/div/app-my-services-container/mat-tab-group/div/mat-tab-body[2]/div/app-my-services-operator-list/div[2]/table/tbody/tr[2]/td/div/app-operator-service-subscription/div/div/div/div[2]/div[2]/div/div/app-card-sm/mat-card/div[1]")
# Save the window opener (current window, do not mistake with tab... not the same).
main_window = driver.current_window_handle
driver = 1
# Open the link in a new tab by sending keystrokes on the element.
actions = ActionChains(driver)
actions.key_down(Keys.COMMAND)
actions.click(first_link)
actions.key_up(Keys.COMMAND)
actions.perform()
driver.switch_to.window(driver.window_handles[1])
time.sleep(5)
# Close current tab.
driver.close()
time.sleep(5)
# Put the focus on the current window which will be the window opener.
driver.switch_to.window(main_window)
# Close the instance of the browser.
driver.quit()
没关系,我解决了。 关键是使用此代码:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import ui
import time
#...
#...
actions = ActionChains(driver)
about = driver.find_element_by_xpath("enter your XPATH")
actions.key_down(Keys.CONTROL).click(about).key_up(Keys.CONTROL).perform()
driver.switch_to.window(driver.window_handles[-1])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.