Python Selenium - 点击基于样式属性或子元素包含文本的css元素? Twitter关注

2024-10-01 02:27:08 发布

您现在位置:Python中文网/ 问答频道 /正文

为了消除任何困惑,我的最终目标是点击所有我还没有关注的用户的follow按钮。在

你好,我已经挂了几个小时了,谢谢你的意见。我试图单击包含文本“Following”的页面上的所有按钮,以避免单击我已经关注的用户的按钮,因为标记的性质以及单击按钮可以跟踪和取消跟踪用户。在

旁注:“user actions follow button js follow btn follow button”的第一个button同级元素将显示属性从显示:无到显示:阻止用户尚未被跟踪。在

我该如何完成这项任务?当我试图在一个错误的项目点击。在

html_list = driver.find_element_by_css_selector(".Grid--withGutter")
items = html_list.find_elements_by_css_selector(".follow-text")

for item in items:
item.click()

到目前为止已经成功了,但是没有区别于已经关注的用户,所以它不关注我已经关注的用户。在

^{pr2}$

这是一个单键的例子,页面中会包含几十个按钮。在

<span class="user-actions-follow-button js-follow-btn follow-button">
  <button type="button" class="EdgeButton EdgeButton--secondary EdgeButton--small button-text follow-text">
    <span aria-hidden="true">Follow</span>
    <span class="u-hiddenVisually">Follow 
      <span class="username u-dir" dir="ltr">@<b>jefken11</b></span>
    </span>
  </button>
  <button type="button" class="EdgeButton EdgeButton--primary EdgeButton--small button-text following-text">
    <span aria-hidden="true">Following</span>
    <span class="u-hiddenVisually">Following 
      <span class="username u-dir" dir="ltr">@<b>jefken11</b></span>
    </span>
  </button>
  <button type="button" class="EdgeButton EdgeButton--danger EdgeButton--small button-text unfollow-text">
    <span aria-hidden="true">Unfollow</span>
  <span class="u-hiddenVisually">Unfollow 
    <span class="username u-dir" dir="ltr">@<b>jefken11</b></span>
    </span>
  </button>
  <button type="button" class="EdgeButton EdgeButton--invertedDanger EdgeButton--small button-text blocked-text">
    <span aria-hidden="true">Blocked</span>
    <span class="u-hiddenVisually">Blocked 
      <span class="username u-dir" dir="ltr">@<b>jefken11</b></span>
    </span>
  </button>
  <button type="button" class="EdgeButton EdgeButton--danger EdgeButton--small button-text unblock-text">
    <span aria-hidden="true">Unblock</span>
    <span class="u-hiddenVisually">Unblock 
      <span class="username u-dir" dir="ltr">@<b>jefken11</b></span>
    </span>
  </button>
  <button type="button" class="EdgeButton EdgeButton--secondary EdgeButton--small button-text pending-text">
    <span aria-hidden="true">Pending</span>
    <span class="u-hiddenVisually">Pending follow request from 
      <span class="username u-dir" dir="ltr">@<b>jefken11</b></span>
    </span>
  </button>
  <button type="button" class="EdgeButton EdgeButton--secondary EdgeButton--small button-text cancel-text">
    <span aria-hidden="true">Cancel</span>
    <span class="u-hiddenVisually">Cancel your follow request to 
      <span class="username u-dir" dir="ltr">@<b>jefken11</b></span>
    </span>
  </button>
</span>

我收到的错误:

>Traceback (most recent call last):
  File "/home/dznutts/PycharmProjects/untitled/twitter_follow.py", line 142, in <module>
    browserWork()
  File "/home/dznutts/PycharmProjects/untitled/twitter_follow.py", line 115, in browserWork
    item.click()
  File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webelement.py", line 80, in click
    self._execute(Command.CLICK_ELEMENT)
  File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webelement.py", line 501, in _execute
    return self._parent.execute(command, params)
  File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webdriver.py", line 308, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: 

Tags: textintruetypedirusernamebuttonhidden
1条回答
网友
1楼 · 发布于 2024-10-01 02:27:08

如果您要做的是单击包含“Following X”的所有可见的BUTTON,那么这些都有一个CSS类following-text。为什么不用这个?在

items = driver.find_elements_by_css_selector("button.following-text")
visible_items = [item for item in items if item.is_displayed()]
for item in visible_items
    item.click()

相关问题 更多 >