首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Selenium Python Click函数不工作

Selenium Python Click函数不工作
EN

Stack Overflow用户
提问于 2021-01-02 04:52:25
回答 2查看 99关注 0票数 0

我对这个有一个很大的问题,我现在不知道该怎么办。

所以,我正在尝试使用selenium来获取关于一种弹出窗口的信息。pop up(这就是弹出窗口,它在tiktok上)

按钮追随者的HTML元素: Followers<

代码语言:javascript
复制
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%的时间,它不是,我真的不知道为什么

提前感谢您的帮助!

EN

回答 2

Stack Overflow用户

发布于 2021-01-02 05:02:15

您应该使用WebDriverWaitelement_to_be_clickable expected等待按钮变为可单击状态。例如:

代码语言:javascript
复制
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() 
票数 0
EN

Stack Overflow用户

发布于 2021-01-02 05:15:37

请试试这个:

代码语言:javascript
复制
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()

我希望这能解决你的问题。

票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65533604

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档