You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
2.7 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from time import sleep
from selenium.webdriver.edge.service import Service
from selenium.webdriver.edge.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
options = webdriver.EdgeOptions()
options.add_argument("--start-maximized") # 最大化窗口
options.add_argument("--disable-notifications") # 禁用通知
options.headless = True
options.add_argument("user-data-dir=C:/Users/JJM/AppData/Local/Microsoft/Edge/User Data/Default") # 使用本地用户数据目录
options= webdriver.EdgeOptions()
options.add_experimental_option("detach", True)
driver_edge = webdriver.Edge(options=options)
my_url = "https://www.douyin.com/"
driver_edge.get(my_url)
sleep(70)
# x=driver_edge.find_element(By.XPATH,'//*[@id="login-pannel"]/div[2]')
# x.click()
# sleep(10)
# 模拟点击
search_box = driver_edge.find_element(By.XPATH,'//*[@id="douyin-header"]/div[1]/header/div/div/div[1]/div/div[2]/div/div/input').send_keys('zy2752629612',Keys.ENTER)
sleep(20)
driver_edge.execute_script("window.scrollTo(0, document.body.scrollHeight);")
sleep(10) # 等待滚动效果
#f=driver_edge.find_element(By.CLASS_NAME,'B3AsdZT9 BMJLsW6X').click()
elements = driver_edge.find_elements(By.XPATH, "//*")
# 检查每个元素是否可见
for element in elements:
if element.is_displayed():
print(f"元素 {element.tag_name} 可见")
else:
print(f"元素 {element.tag_name} 不可见")
driver_edge.execute_script("arguments[0].style.display = 'block'; arguments[0].style.visibility = 'visible';", element)
try:
# 等待元素可见超时设置为5秒
WebDriverWait(driver_edge, 5).until(
EC.visibility_of(element)
)
print(f"元素 {element.tag_name} 现在可见")
except TimeoutException:
print(f"元素 {element.tag_name} 仍然不可见")
driver_edge.execute_script("arguments[0].scrollIntoView(true);", element)
sleep(10) # 等待滚动效果
# 再次检查元素是否可见
if element.is_displayed():
print("元素现在可见")
else:
print("元素仍不可见")
sleep(10)
h=driver_edge.find_element(By.XPATH,'//*[@id="douyin-right-container"]/div[2]/div/div/div[2]/div[3]/div[3]/div[1]/button[2]')
h.click()
# 获取cookie
cookies = driver_edge.get_cookies()
for cookie in cookies:
print(cookie)
# 关闭浏览器
driver_edge.quit()