我对这个有一个很大的问题,我现在不知道该怎么办。
所以,我正在尝试使用selenium来获取关于一种弹出窗口的信息。pop up(这就是弹出窗口,它在tiktok上)
按钮追随者的HTML元素: Followers<
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
driver = webdriver.Firefox(executable_path='./geckodriver')
driver.get("https://www.tiktok.com/@programm___r?lang=en") # on lance tiktok sur ordi
# first thing first, just click on the button that "launch" the pop up
driver.find_element_by_class_name('jsx-1737374796.header-inbox-icon').click()
# Then, I want to click on "Followers" category but this is getting complicated here
# First idea to click on the category, check if it contains the word "followers"
if (driver.find_element_by_xpath('/html/body/div[1]/div/div[1]/div/div[3]/div[2]/div[2]/div/div[1]/div/span[5]').text) == "Followers" : # this line works, no problem
driver.find_element_by_xpath('/html/body/div[1]/div/div[1]/div/div[3]/div[2]/div[2]/div/div[1]/div/span[5]').click() # this one doesn't work i don't know why
# So, second idea, try with the webdriverwait
WebDriverWait(driver,20).until(EC.presence_of_element_located((By.XPATH,'/html/body/div[1]/div/div[1]/div/div[3]/div[2]/div[2]/div/div[1]/div/span[5]'))) # this works
driver.find_element_by_xpath('/html/body/div[1]/div/div[1]/div/div[3]/div[2]/div[2]/div/div[1]/div/span[5]').click() # this still doesn't work
# Third idea, instead o xpath, css-selector
WebDriverWait(driver,20).until(EC.presence_of_element_located((By.CSS_SELECTOR,"span.jsx-2344352055:nth-child(5)"))) # work, no problem
driver.find_element_by_css_selector("span.jsx-2344352055:nth-child(5)").click() # doesn't work neither..
# Fourth and last idea, probably the least fine, get all the elements with the class name, and only one return Followers with the .text, but still the same problem
elements = (driver.find_elements_by_class_name("jsx-2344352055"))
for i in range(len(elements)) :
if elements[i].text == "Followers" :
elements[i].click() # but still doesn't work
有时,它起作用了,就像我不知道为什么或如何,但有时,点击起作用了,但就像95%的时间,它不是,我真的不知道为什么
提前感谢您的帮助!
发布于 2021-01-01 21:02:15
您应该使用WebDriverWait
和element_to_be_clickable
expected等待按钮变为可单击状态。例如:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
driver = webdriver.Firefox(executable_path='./geckodriver')
driver.get("https://www.tiktok.com/@programm___r?lang=en")
WebDriverWait(driver,20).until(EC.presence_of_element_located((By.XPATH,'/html/body/div[1]/div/div[1]/div/div[3]/div[2]/div[2]/div/div[1]/div/span[5]')))
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,'/html/body/div[1]/div/div[1]/div/div[3]/div[2]/div[2]/div/div[1]/div/span[5]'))).click()
发布于 2021-01-01 21:15:37
请试试这个:
driver = webdriver.Firefox(executable_path='./geckodriver')
driver.get("https://www.tiktok.com/@programm___r?lang=en") # on lance tiktok sur ordi
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS) ;
# first thing first, just click on the button that "launch" the pop up
driver.find_element_by_class_name('jsx-1737374796.header-inbox-icon').click()
我希望这能解决你的问题。
https://stackoverflow.com/questions/65533604
复制