Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

problems while using the selenium in python3 #167

Open
YYSFZRW1 opened this issue Sep 6, 2024 · 0 comments
Open

problems while using the selenium in python3 #167

YYSFZRW1 opened this issue Sep 6, 2024 · 0 comments
Labels
feedback Feedback, discussion, or question about EdgeDriver

Comments

@YYSFZRW1
Copy link

YYSFZRW1 commented Sep 6, 2024

Here’s the English translation of your text:

The issue I’m encountering is as follows: I’m trying to write a semi-automated Python script that uses the Selenium library to access the history page in the Edge browser, input a specified string for searching, and delete the results obtained after the search. However, I’m having trouble correctly navigating to the user’s history page. The code that GPT helped me write only accesses a history page unrelated to the user, rather than the user’s actual history page. Later, I attempted to access just the user’s history page, but that also failed. Could you provide me with some advice?
my code is as following:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
from selenium.webdriver.edge.service import Service # 导入 Service 类
import time

def check_edge_history_page():
options = webdriver.EdgeOptions()
# 指定Edge浏览器的路径
options.binary_location = r"C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe"

# 添加用户数据路径 (替换为你实际的 Edge 用户配置路径)
options.add_argument(r"user-data-dir=C:/Users/ASUS/AppData/Local/Microsoft/Edge/User Data")

# 指定 EdgeDriver 的路径
service = Service(executable_path=r"D:/PYTHON312/Scripts/edgedriver_win64/MicrosoftWebDriver.exe")  # 修正路径并使用 Service

# 使用 Edge WebDriver 和服务
driver = webdriver.Edge(service=service, options=options)

# 打开历史记录页面
history_url = "edge://history/all"
driver.get(history_url)

# 等待页面加载 (根据实际情况调整时间)
time.sleep(5)

# 保存页面截图,检查页面是否正确加载
driver.save_screenshot("edge_history_page.png")
print("已保存页面截图为 'edge_history_page.png'")

# 输出页面源代码,可以检查自动化脚本加载的结果
print(driver.page_source)

# 关闭浏览器
driver.quit()

if name == "main":
check_edge_history_page()

'''
def confirm_action():
confirmation = input("确认删除与该字符串相关的历史记录吗?(y/n): ")
return confirmation.lower() == 'y'

def delete_edge_history(search_string):
# 使用 Edge 浏览器驱动
driver = webdriver.Edge() # 请确保你已经下载了 Edge WebDriver 并设置到环境变量,或指定路径
search_url = f"edge://history/all?q={search_string}"
# 打开 Edge 历史记录的搜索结果页面
driver.get(search_url)

# 增加等待时间,确保页面完全加载
time.sleep(5)  # 你可以根据页面加载的情况增加或减少这个等待时间

# 使用显式等待来确保删除按钮加载出来
try:
    # 等待搜索结果中的删除按钮可见,最多等待 15 秒
    search_results = WebDriverWait(driver, 15).until(
        EC.presence_of_all_elements_located((By.XPATH, "//button[contains(@aria-label, '删除')]"))
    )

    if search_results:
        print(f"找到{len(search_results)}个与 '{search_string}' 相关的历史记录。")
        for result in search_results:
            result.click()  # 删除该历史记录
            time.sleep(1)  # 给每次删除留点时间
        print(f"已删除所有与 '{search_string}' 相关的历史记录。")
    else:
        print(f"没有找到与 '{search_string}' 相关的历史记录。")

except Exception as e:
    print("未找到任何与该字符串相关的历史记录,或者页面加载超时。")
    print(e)

# 关闭浏览器
driver.quit()

if name == "main":
search_string = input("请输入要删除的字符串: ")#input the string which you want to deleted in edge's history
if confirm_action():
delete_edge_history(search_string)
else:
print("操作已取消。")
'''

@YYSFZRW1 YYSFZRW1 added the feedback Feedback, discussion, or question about EdgeDriver label Sep 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feedback Feedback, discussion, or question about EdgeDriver
Projects
None yet
Development

No branches or pull requests

1 participant