For Edge, when you want use "--user-data-dir". Just go to edge://version to check. "C:\Users\Plane\AppData\Local\Microsoft\Edge\User Data\Profile 1". So when set options.addargument("--profile-directory=[]"), It may not be the "Default".

This commit is contained in:
Pyhtagodzilla 2025-05-30 08:41:46 +08:00
commit 86e245581a
9 changed files with 128 additions and 0 deletions

8
.idea/.gitignore generated vendored Normal file
View File

@ -0,0 +1,8 @@
# 默认忽略的文件
/shelf/
/workspace.xml
# 基于编辑器的 HTTP 客户端请求
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

8
.idea/BingPoints.iml generated Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="jdk" jdkName="BingPoints" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@ -0,0 +1,10 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="SpellCheckingInspection" enabled="true" level="INFORMATION" enabled_by_default="true">
<option name="processCode" value="true" />
<option name="processLiterals" value="true" />
<option name="processComments" value="true" />
</inspection_tool>
</profile>
</component>

View File

@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>

7
.idea/misc.xml generated Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Black">
<option name="sdkName" value="BingPoints" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="BingPoints" project-jdk-type="Python SDK" />
</project>

8
.idea/modules.xml generated Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/BingPoints.iml" filepath="$PROJECT_DIR$/.idea/BingPoints.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml generated Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

22
run.py Normal file
View File

@ -0,0 +1,22 @@
from utils import init_driver_edge
from selenium.webdriver.common.by import By
import time
def test() -> None:
driver = init_driver_edge()
driver.get("http://baidu.com")
def do_check_in():
driver = init_driver_edge()
time.sleep(10)
driver.get("https://cn.bing.com/rewards/panelflyout?channel=bingflyout&partnerId=BingRewards&isDarkMode=0&ru=https%3A%2F%2Fcn.bing.com%2F")
driver.maximize_window()
cont = driver.find_elements(By.CLASS_NAME,"promo_cont")
for i in cont:
if i.get_attribute('role') == 'banner' or i.get_attribute('id') == 'exclusive_promo_cont':
i.click()
time.sleep(10)
if __name__ == "__main__":
do_check_in()

53
utils.py Normal file
View File

@ -0,0 +1,53 @@
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
service = Service(executable_path="/usr/bin/chromedriver")
options = Options()
options.add_argument("--headless") # Run in headless mode
options.add_argument("--no-sandbox")
# 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
# from selenium import webdriver
# from selenium.webdriver.edge.service import Service
# from time import sleep
#
# ser = Service("E:\\webdriver\\msedgedriver.exe")
# edge_options = webdriver.EdgeOptions()
# edge_options.use_chromium = True
# edge_options.add_argument("user-data-dir=C:\\Users\\Administrator\\AppData\\Local\\Microsoft\\Edge\\User Data")
# edge_options.add_argument("profile-directory=Profile 1")
#
# driver = webdriver.Edge(service = ser,options = edge_options)
# driver.get('https://www.bing.com')
#
# sleep(20)