簡體   English   中英

我有一個xpath列表,如何在不同的選項卡中打開它們?

[英]I have a list of xpaths, how can I open each one of them in a different tab?

基本上,我有一個網站,我需要打開幾個標簽,並在每個標簽中做一些事情。 我怎么能用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循環中訪問每個選項卡並執行您想要的操作,添加您希望驅動程序執行的代碼。 如果您無法理解代碼的任何部分,請發表評論。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM