我有一个 xpath 列表,如何在不同的选项卡中打开每个 xpath



基本上,我有一个网站,我需要在其中打开几个选项卡并在每个选项卡中执行一些操作。如何使用Python + Selenium和Chrome来做到这一点?这是我的代码:

from selenium import webdriver
import os
import time
import pyautogui
option = webdriver.ChromeOptions()
chrome_prefs = {}
option.experimental_options["prefs"] = chrome_prefs
chrome_prefs["profile.default_content_settings"] = {"images": 2}
chrome_prefs["profile.managed_default_content_settings"] = {"images": 2}
chrome_prefs["profile.default_content_settings"] = { "popups": 2 }
option.add_argument("--disable-notifications")

    driver = webdriver.Chrome(chrome_options=option)
    driver.get('https://www.spiritfanfiction.com/login')
    driver.find_element_by_xpath("//*[@id='Usuario']").send_keys("breakfast")
    driver.find_element_by_xpath("//*[@title='Senha']").send_keys("302290679")
    driver.find_element_by_xpath("//*[@class='btn btn-primary']").send_keys("302290679")
    driver.find_element_by_xpath("//*[@class='btn btn-primary']").click()
#this opens the page I need to get all the xpaths from 
    LinkDoPerfil = driver.get("https://www.spiritfanfiction.com/recentes?pagina=1000")
#this is the xpath I need to open in each tab
    transactionElements = driver.find_elements_by_xpath("//*[@class='usuario usuarioPopupTrigger link']")
#this is the part I have no idea what I'm doing lol. But it was supposed to open all the xpaths in a different tab.      
    for element in transactionElements:
            ActionChains(driver) 
                .key_down(Keys.CONTROL) 
                .click(element) 
                .key_up(Keys.CONTROL) 
                .perform()
#this switches to the latest opened tab, which is the final xpath I got from the page.
    driver.switch_to_window(driver.window_handles[-1])
    try:
            driver.find_element_by_xpath('//*[@class="fa fa-eye"]').click()
    except:
            browser.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 'w')

有人可以告诉我出了什么问题吗?它以前有效,但我搞砸了,现在不是了。之后什么都不会发生:

LinkDoPerfil = driver.get("https://www.spiritfanfiction.com/recentes?pagina=1000")

我可以看到您没有导入某些类,并且我还对您的代码进行了一些更改。

#import libraries
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium import webdriver
import os
import time
# set options for web driver
option = webdriver.ChromeOptions()
chrome_prefs = {}
option.experimental_options["prefs"] = chrome_prefs
chrome_prefs["profile.default_content_settings"] = {"images": 2}
chrome_prefs["profile.managed_default_content_settings"] = {"images": 2}
chrome_prefs["profile.default_content_settings"] = { "popups": 2 }
option.add_argument("--disable-notifications")
driver = webdriver.Chrome(chrome_options=option)
driver.get('https://www.spiritfanfiction.com/login')
driver.find_element_by_xpath("//*[@id='Usuario']").send_keys("breakfast")
driver.find_element_by_xpath("//*[@title='Senha']").send_keys("302290679")
driver.find_element_by_xpath("//*[@class='btn btn-primary']").send_keys("302290679")
driver.find_element_by_xpath("//*[@class='btn btn-primary']").click()
LinkDoPerfil = driver.get("https://www.spiritfanfiction.com/recentes?pagina=1000")
transactionElements = driver.find_elements_by_xpath("//*[@class='usuario usuarioPopupTrigger link']")
for element in transactionElements:
            ActionChains(driver) 
                .key_down(Keys.CONTROL) 
                .click(element) 
                .key_up(Keys.CONTROL) 
                .perform()
# Store all the tabs in the variable
tabs = driver.window_handles
# Switch to each tab opened one by one
for x in tabs[1:]:
    time.sleep(2)
    driver.switch_to_window(x)
    '''
    Do whatever you want in each tab here
    '''

在 for 循环中,访问每个选项卡并执行所需的操作,添加希望驱动程序执行的代码。如果您在理解代码的任何部分时遇到问题,请发表评论。

最新更新