获取一组链接?

2024-06-30 07:55:15 发布

您现在位置:Python中文网/ 问答频道 /正文

如何使用driver.get在Chrome中打开多个URL

我的代码:

import requests
import json
import pandas as pd
from selenium import webdriver
chromeOptions = webdriver.ChromeOptions()
chromedriver = r"C:\Users\Harrison Pollock\Downloads\Python\chromedriver_win32\chromedriver.exe"
driver = webdriver.Chrome(executable_path=r"C:\Users\Harrison Pollock\Downloads\Python\chromedriver_win32\chromedriver.exe",chrome_options=chromeOptions)
links = []
request1 = requests.get('https://api.beta.tab.com.au/v1/recommendation-service/featured-events?jurisdiction=NSW')
json1 = request1.json()
for n in json1['nextToGoRaces']:
    if n['meeting']['location'] in ['VIC','NSW','QLD','SA','WA','TAS','IRL']:
        links.append(n['_links']['self'])
driver.get('links')

Tags: importjsongetdownloadsdriverlinkschromerequests
1条回答
网友
1楼 · 发布于 2024-06-30 07:55:15

基于这些注释-您需要一个类来管理您的浏览器,一个用于测试的类,然后一个并行运行的运行程序

试试这个:

import unittest
import time
import testtools
from selenium import webdriver

class BrowserManager:
    browsers=[]
    def createBrowser(self, url):
        browser = webdriver.Chrome()
        browser.get(url)
        self.browsers.append(browser)

    def getBrowserByPartialURL(self, url):
        for browser in self.browsers:
            if url in browser.current_url:
                return browser

    def CloseItAllDown(self):
        for browser in self.browsers:
            browser.close()



class UnitTest1(unittest.TestCase):
    def test_DoStuffOnGoogle(self):
        browser = b.getBrowserByPartialURL("google")
        #Point of this is to watch the output! you'll see this +other test intermingled (proves parallel run)
        for i in range(10):
            print(browser.current_url)
            time.sleep(1)
 
    def test_DoStuffOnYahoo(self):
        browser = b.getBrowserByPartialURL("yahoo")
        #Point of this is to watch the output! you'll see this +other test intermingled (proves parallel run)
        for i in range(10):
            print(browser.current_url)
            time.sleep(1)



#create a global variable for the brwosers
b = BrowserManager()

# To Run the tests
if __name__ == "__main__":
    ##move to an init to Create your browers
    b.createBrowser("https://www.google.com")
    b.createBrowser("https://www.yahoo.com")

    time.sleep(5) # This is so you can see both open at the same time

    suite = unittest.TestLoader().loadTestsFromTestCase(UnitTest1)
    concurrent_suite = testtools.ConcurrentStreamTestSuite(lambda: ((case, None) for case in suite))
    concurrent_suite.run(testtools.StreamResult())

这段代码没有做任何令人兴奋的事情——它是一个如何管理多个浏览器和并行运行测试的示例。它转到指定的URL(您应该移动到init/setup),然后打印出它所在的URL 10次

这是向管理器添加浏览器的方式:b.createBrowser("https://www.google.com")

这是您检索浏览器的方式:browser = b.getBrowserByPartialURL("google")-注意它是一个部分URL,因此您可以将域用作关键字

这是输出(只是前几行-不是全部…)-它是谷歌的打印URL,然后是雅虎,然后是谷歌,然后是雅虎-显示它们同时运行:

PS C:\Git\PythonSelenium\BrowserManager>  cd 'c:\Git\PythonSelenium'; & 'C:\Python38\python.exe' 'c:\Users\User\.vscode\extensions\ms-python.python-2020.7.96456\pythonFiles\lib\python\debugpy\launcher' '62426' ' ' 'c:\Git\PythonSelenium\BrowserManager\BrowserManager.py' 
DevTools listening on ws://127.0.0.1:62436/devtools/browser/7260dee3-368c-4f21-bd59-2932f3122b2e
DevTools listening on ws://127.0.0.1:62463/devtools/browser/9a7ce919-23bd-4fee-b302-8d7481c4afcd

https://www.google.com/
https://consent.yahoo.com/collectConsent?sessionId=3_cc-session_d548b656-8315-4eef-bb1d-82fd4c6469f8&lang=en-GB&inline=false
https://www.google.com/
https://consent.yahoo.com/collectConsent?sessionId=3_cc-session_d548b656-8315-4eef-bb1d-82fd4c6469f8&lang=en-GB&inline=false
https://www.google.com/

相关问题 更多 >