This commit is contained in:
Pyhtagodzilla 2025-07-26 10:39:37 +08:00
parent 21783d06af
commit 214a8c6c74
3 changed files with 76 additions and 54 deletions

23
run.py
View File

@ -1,13 +1,24 @@
import time
import selenium.common
from utils import Bing
from utils import init_driver_edge, init_driver_chrome
from utils import get_points
User_profile_chrome = ["chromium"]
User_profile_edge = ["User Data","User DataBM"]
def main():
driver = init_driver_chrome()
get_points(driver=driver)
time.sleep(10)
get_points(driver=driver)
for path in User_profile_edge:
try:
driver = Bing.init_driver_edge(path)
Bing.get_points(driver=driver)
time.sleep(10)
Bing.get_points(driver=driver)
time.sleep(10)
driver.quit()
print("quit")
except selenium.common.exceptions.WebDriverException as e:
print(e)
if __name__ == "__main__":
main()

4
test_profile.py Normal file
View File

@ -0,0 +1,4 @@
from utils import Bing
driver = Bing.init_driver_edge("User DataXL")
driver.get("https://www.bing.com/")

103
utils.py
View File

@ -1,58 +1,65 @@
from selenium import webdriver
def init_driver_edge():
"""Initialize the Selenium WebDriver with Edge edge_options.
Returns:
WebDriver: The initialized Selenium WebDriver instance.
"""
from selenium.webdriver.edge.service import Service
from selenium.webdriver.edge.options import Options
service = Service(r"D:\Projects\BUCT\edgedriver\msedgedriver.exe")
edge_options = Options()
# edge_options.add_argument("--headless") # Run in headless mode
edge_options.add_argument("--no-sandbox")
edge_options.add_experimental_option("detach", False)
edge_options.add_argument("--user-data-dir=C:\\Users\\Plane\\AppData\\Local\\Microsoft\\Edge\\User Data")
# edge_options.add_argument(r"user-data-dir=")
edge_options.add_argument("--profile-directory=Profile 1")
driver = webdriver.Edge(options=edge_options, service=service)
driver.implicitly_wait(5) # Implicit wait for elements to load
return driver
def init_driver_chrome():
"""Initialize the Selenium WebDriver with Chrome options.
Returns:
WebDriver: The initialized Selenium WebDriver instance.
"""
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
class Bing:
service = Service(executable_path="/usr/bin/chromedriver")
options = Options()
options.add_argument("--headless") # Run in headless mode
options.add_argument("--no-sandbox")
options.add_argument('--user-data-dir=/home/pythagodzilla/.config/chromium')
# options.add_experimental_option("detach", True)
driver = webdriver.Chrome(service=service, options=options)
driver.implicitly_wait(5) # Implicit wait for elements to load
return driver
@staticmethod
def init_driver_edge(profile_path: str):
"""Initialize the Selenium WebDriver with Edge edge_options.
Returns:
WebDriver: The initialized Selenium WebDriver instance.
"""
from selenium.webdriver.edge.service import Service
from selenium.webdriver.edge.options import Options
service = Service(r"D:\Projects\BUCT\edgedriver\msedgedriver.exe")
edge_options = Options()
# edge_options.add_argument("--headless") # Run in headless mode
edge_options.add_argument("--no-sandbox")
edge_options.add_experimental_option("detach", False)
edge_options.add_argument(f"--user-data-dir=C:\\Users\\Plane\\AppData\\Local\\Microsoft\\Edge\\{profile_path}")
# edge_options.add_argument(f"--user-data-dir=C:\\Users\\Plane\\AppData\\Local\\Microsoft\\Edge\\User Data")
# edge_options.add_argument(r"user-data-dir=")
edge_options.add_argument("--profile-directory=Profile 1")
driver = webdriver.Edge(options=edge_options, service=service)
driver.implicitly_wait(5) # Implicit wait for elements to load
return driver
def get_points(driver) -> None:
"""
Open the rewards page and click all add-points elements.
"""
import time
from selenium.webdriver.common.by import By
driver.get(
"https://cn.bing.com/rewards/panelflyout?channel=bingflyout&partnerId=BingRewards&isDarkMode=0&ru=https%3A%2F%2Fcn.bing.com%2F")
@staticmethod
def init_driver_chrome(profile_path: str):
"""Initialize the Selenium WebDriver with Chrome options.
Returns:
WebDriver: The initialized Selenium WebDriver instance.
"""
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
driver.maximize_window()
service = Service(executable_path="/usr/bin/chromedriver")
options = Options()
options.add_argument("--headless") # Run in headless mode
options.add_argument("--no-sandbox")
options.add_argument(f'--user-data-dir=/home/pythagodzilla/.config/{profile_path}')
# options.add_argument('--user-data-dir=/home/pythagodzilla/.config/chromium')
# options.add_experimental_option("detach", True)
driver = webdriver.Chrome(service=service, options=options)
driver.implicitly_wait(5) # Implicit wait for elements to load
return driver
cont = driver.find_elements(By.CLASS_NAME, "promo_cont") # 查找classpromo_cont。但对应多个可点击元素
@staticmethod
def get_points(driver):
"""
Open the rewards page and click all add-points elements.
"""
import time
from selenium.webdriver.common.by import By
driver.get(
"https://cn.bing.com/rewards/panelflyout?channel=bingflyout&partnerId=BingRewards&isDarkMode=0&ru=https%3A%2F%2Fcn.bing.com%2F")
for i in cont:
if i.get_attribute('role') == 'banner' or i.get_attribute('id') == 'exclusive_promo_cont':
i.click() # 点击所有符合元素
driver.maximize_window()
cont = driver.find_elements(By.CLASS_NAME, "promo_cont") # 查找classpromo_cont。但对应多个可点击元素
time.sleep(10) # 等待页 面完全加载。
for i in cont:
if i.get_attribute('role') == 'banner' or i.get_attribute('id') == 'exclusive_promo_cont':
i.click() # 点击所有符合元素
time.sleep(10) # 等待页 面完全加载。