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("--headless") options.add_argument("--start-maximized") # 最大化窗口 options.add_argument("--disable-notifications") # 禁用通知 options.add_argument("user-data-dir=C:/Users/JJM/AppData/Local/Microsoft/Edge/User Data/Default") # 使用本地用户数据目录 options.add_experimental_option("detach", True) driver_edge = webdriver.Edge(options=options) cookies = driver_edge.get_cookies() for cookie in cookies: print(cookie) my_url = "https://www.douyin.com/" driver_edge.get(my_url) sleep(5) # 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) search_box = driver_edge.find_element(By.XPATH,'//input[@class="st2xnJtZ YIde9aUh"]') print(search_box) search_box.send_keys('zy2752629612',Keys.ENTER) sleep(5) print('===========>111111111') handles = driver_edge.window_handles #获取当前浏览器的所有窗口句柄 driver_edge.switch_to.window(handles[-1]) divm = driver_edge.find_element(By.XPATH,'//div[@class="G8LTciwG"]/a') divm.click() print('==============66666') # sleep(10) # driver_edge.execute_script("window.scrollTo(0, document.body.scrollHeight);") # sleep(1) # 等待滚动效果 #f=driver_edge.find_element(By.CLASS_NAME,'B3AsdZT9 BMJLsW6X').click() # elements = driver_edge.find_elements(By.XPATH, "//*") # print(elements) # 检查每个元素是否可见 # 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, 1).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(1) # 等待滚动效果 # # 再次检查元素是否可见 # if element.is_displayed(): # print("元素现在可见") # else: # print("元素仍不可见") sleep(10) handles = driver_edge.window_handles #获取当前浏览器的所有窗口句柄 driver_edge.switch_to.window(handles[-1]) print('==============66666') h=driver_edge.find_element(By.XPATH,'//div[@class="hLIm2dFu"]/button[2]') h.click() b=driver_edge.find_element(By.XPATH,'//div[@class="c8uBfaOs"]/div[@class="public-DraftEditorPlaceholder-inner"]') b.send_keys('zy2752629612',Keys.ENTER) # # 关闭浏览器 # driver_edge.quit()