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

时间:2019-04-20 23:49:28

标签: python selenium google-chrome

基本上,我有一个网站,我需要在其中打开几个选项卡,并在每个选项卡中执行一些操作。如何使用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")

1 个答案:

答案 0 :(得分:0)

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

#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循环内,访问每个选项卡并执行所需的操作,添加要驱动程序执行的代码。如果您无法理解代码的任何部分,请发表评论。