简体   繁体   English

Selenium Python 点击 function 不起作用

[英]Selenium Python Click function doesn't work

I'm having a big problem with this one and I just don't know what to do now.我对这个有很大的问题,我现在不知道该怎么办。

So, I'm trying to use selenium to get an information on a sort of a pop up.所以,我正在尝试使用 selenium 来获取有关某种弹出窗口的信息。 pop up (This is this exact pop-up, it's on tiktok)弹出(这是这个确切的弹出窗口,它在 tiktok 上)

The HTML element of the button followers: Followers<按钮关注者的 HTML 元素:关注者<

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 

Sometimes, it worked, like I don't know why or how but sometimes, the click works, but like 95% of the time, it doesn't and I really don't know why有时,它有效,就像我不知道为什么或如何,但有时,点击有效,但就像 95% 的时间,它没有,我真的不知道为什么

Thanks in advance for your help !在此先感谢您的帮助 !

You should wait for the button to be clickable using WebDriverWait and element_to_be_clickable expected condition.您应该使用WebDriverWaitelement_to_be_clickable预期条件等待按钮可点击。 For example:例如:

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() 

Please try this one:请试试这个:

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()

I hope this resolves your issue.我希望这可以解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM