Python Selenium-基于样式属性单击css元素还是包含子元素的文本?推特关注

Python Selenium-基于样式属性单击css元素还是包含子元素的文本?推特关注,python,css,selenium,twitter,Python,Css,Selenium,Twitter,为了消除任何混乱,我的最终目标是单击我尚未跟踪的用户的所有跟踪按钮 您好,我已经挂了几个小时了,非常感谢您的意见。我试图单击包含文本“Following”的页面上的所有按钮,以避免单击我已经在跟踪的用户的按钮,因为标记的性质以及单击按钮如何跟踪和取消跟踪用户 旁注:“用户操作跟随按钮js跟随btn跟随按钮”的第一个按钮同级元素将显示属性从display:none更改为display:block(当用户尚未跟随时) 我将如何着手完成这项任务?我尝试单击项目时出错 html_list = drive

为了消除任何混乱,我的最终目标是单击我尚未跟踪的用户的所有跟踪按钮

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

旁注:“用户操作跟随按钮js跟随btn跟随按钮”的第一个按钮同级元素将显示属性从display:none更改为display:block(当用户尚未跟随时)


我将如何着手完成这项任务?我尝试单击项目时出错

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()
到目前为止,它已经起到了作用,但并没有区别于已经关注的用户,所以它将解除我已经关注的用户的关注

driver.find_elements_by_css_selector('.user-actions-follow-button')[count].click()
这是一个单一按钮的示例,页面将包含几十个按钮

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

跟随
跟随
@杰夫肯11
下列的
下列的
@杰夫肯11
展开
展开
@杰夫肯11
此 路 不通
此 路 不通
@杰夫肯11
解除封锁
解除封锁
@杰夫肯11
悬而未决的
来自的挂起跟随请求
@杰夫肯11
取消
取消您的跟踪请求,以
@杰夫肯11
我收到的错误是:

>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: 
>回溯(最近一次呼叫最后一次):
文件“/home/dznutts/PycharmProjects/untitled/twitter_follow.py”,第142行,in
browserWork()
文件“/home/dznutts/PycharmProjects/untitled/twitter_follow.py”,第115行,在browserWork中
项目。单击()
文件“/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webelement.py”,第80行,单击
self.\u执行(命令。单击\u元素)
文件“/usr/local/lib/python3.5/dist packages/selenium/webdriver/remote/webelement.py”,第501行,在
返回self.\u parent.execute(命令,参数)
文件“/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webdriver.py”,执行中的第308行
self.error\u handler.check\u响应(响应)
文件“/usr/local/lib/python3.5/dist packages/selenium/webdriver/remote/errorhandler.py”,第194行,在check_响应中
引发异常类(消息、屏幕、堆栈跟踪)
selenium.common.exceptions.ElementNotInteractiableException:消息:

如果您要做的是单击所有包含“Following X”的可见
按钮
s,那么这些按钮都有一个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()

“我尝试单击项目时出错。”带有回溯功能的完整错误消息是什么?感谢您的时间,我已更新了错误。在英语中,您想做什么?跟随你所有的追随者还是???我仍然不知道你在尝试什么。我正在尝试跟踪所有没有被跟踪的用户。非常感谢你的回答。我对python非常陌生。在执行self.error\u handler.check\u response(response)文件“/usr/local/lib/python3.5/dist packages/selenium/webdriver/remote/errorhandler.py”中的第308行,在check\u response引发异常类(消息、屏幕、堆栈跟踪)中,第194行selenium.common.exceptions.ElementNotInteractivatableException:Message:尝试过但没有骰子。我认为这是由于其中一个元素不可见,我已更新代码以仅将元素列表过滤到可见的元素。尝试一下。非常感谢您的努力,将对此进行尝试并进行报告。此代码有效,但仍然有效ll去点击每一个follow按钮,这会导致用户取消之前的follow,并显示原始问题。