【问题标题】:how to click on a webpage all buttons with specific text, one by one如何在网页上单击所有带有特定文本的按钮,一一
【发布时间】:2018-08-28 09:33:33
【问题描述】:

我有this quora 网页作为示例

它确实包含 4 个“查看支持者”按钮 使用此代码,我想获取所有 4 个按钮,以便以后一一单击。

upvoter_list = wait.until(EC.presence_of_all_elements_located((By.CLASS_NAME, 'AnswerVoterListModalLink')))

print(len(upvoter_list)) 给我 4,这是正确的 当我打印upvoter_list 的第一个元素的文本时

print(upvoter_list[0].text) 

结果我得到“查看支持者” 但是当我打印剩余的部分时,我得到的是空结果,

print(upvoter_list[1].text)

有什么问题?

【问题讨论】:

  • 我只看到 2 个按钮和按预期打印的文本
  • 不,有 4 个查看 upvotes 按钮,当您打印 upvoter_list 的第一个元素的文本时,您会得到“view upvotes”,但剩下的结果是空的

标签: python api selenium web-scraping quora


【解决方案1】:

使用这个(更新)

from selenium import webdriver 
from time import sleep
from bs4 import BeautifulSoup
obj = webdriver.Chrome('path to driver')


list_of_upvoters = []
obj.get('https://www.quora.com/Have-you-ever-countined-to-pursue-someone-who-wasnt-interested-in-you')
sleep(5)
obj.execute_script("window.scrollTo(0, document.body.scrollHeight);")
sleep(5)
obj.execute_script("window.scrollTo(0,1);")
sleep(5)

for p in obj.find_elements_by_class_name('AnswerVoterListModalLink'):
    sleep(5)
    p.click()
    sleep(10)
    for div in obj.find_elements_by_class_name('author_info'):
        list_of_upvoters.append(div.find_element_by_class_name('user').text)
    print(list_of_upvoters)
    list_of_upvoters = []
    sleep(10)
    obj.find_element_by_class_name('modal_close').click()
    sleep(10)

【讨论】:

  • 感谢代码,我必须添加什么才能获得点击事件生成的内容?在这种情况下,这是投票者的名字
  • 请注意 p.click () 给了我错误代码,元素不可点击
【解决方案2】:

尝试get_attribute("textContent") 而不是text

How to get inner text of an element in Selenium?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-21
    • 1970-01-01
    • 2017-06-24
    • 1970-01-01
    • 1970-01-01
    • 2014-09-10
    • 2022-06-30
    • 2023-04-02
    相关资源
    最近更新 更多