Instagram / Selenium没有点击接受按钮

时间:2018-02-02 18:30:19

标签: python selenium xpath

我试图制作一个接受追随者请求的简单python脚本。我有一切工作到最后一部分,我需要点击的按钮没有点击。

import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select

"""
Credentials to login to Instagram.
"""

username = "username"
password = "password"

"""
Driver finds the path where the Chrome Driver is located.
getDriver stores the URL.
driver.get loads the URL into Chrome.
"""

driver = webdriver.Chrome(r'path to chrome driver')
getDriver = ("https://www.instagram.com/accounts/activity?followRequests=")
driver.get(getDriver)

"""
Loging into Instagram.
"""

driver.find_element_by_xpath("//input[@name='username']").send_keys(username)
driver.find_element_by_xpath("//input[@name='password']").send_keys(password)
driver.find_element_by_xpath("//button[contains(.,'Log in')]").click()

"""
Accepts the followers. Click button. Then loops until all are accepted.
"""
elements = driver.find_elements_by_class_name('_qv64e _gexxb _4tgw8 _njrw0')
for e in elements:
        e.click()
        time.sleep(2)

"""
Class name: <button class="_qv64e _gexxb _4tgw8 _njrw0">Approve</button>
Xpath name: //*[@id="react-root"]/section/main/section/ul/li[2]/div[3]/div/span[1]/button
"""

我尝试过使用类名和xpath但仍然没有运气。任何帮助是极大的赞赏。

IDLE输出:

RESTART: C:\Users\Tyler\Documents\Programming\Python\AutoAccept\autoAccept.py 
Traceback (most recent call last):
  File "C:\Users\Tyler\Documents\Programming\Python\AutoAccept\autoAccept.py", line 34, in <module>
    elements = driver.find_elements_by_class_name('_qv64e _gexxb _4tgw8 _njrw0')
  File "C:\Users\Tyler\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 571, in find_elements_by_class_name
    return self.find_elements(by=By.CLASS_NAME, value=name)
  File "C:\Users\Tyler\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 983, in find_elements
    'value': value})['value'] or []
  File "C:\Users\Tyler\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 312, in execute
    self.error_handler.check_response(response)
  File "C:\Users\Tyler\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 237, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: Compound class names not permitted
  (Session info: chrome=63.0.3239.132)
  (Driver info: chromedriver=2.35.528161 (5b82f2d2aae0ca24b877009200ced9065a772e73),platform=Windows NT 10.0.15063 x86_64)

按钮的HTML:

<div class="_n76vw"> == $0
        <span class="_4t1rg _ov9ai"> == $0
                <button class="_qv64e _gexxb _4tgw8 _njrw0">Approve</button> == $0
        </span>
</div>

2 个答案:

答案 0 :(得分:0)

现在,selenium不支持多个类名。如果您的类名包含空格,WebDriver会将其视为“复合选择器”。您可以使用cssSelector或id来选择webelement。

尝试以下代码

你可以尝试这个xpath:

//button[@class="_qv64e _gexxb _4tgw8 _njrw0" and text()="Approve"]

或CSS选择器:

._qv64e._gexxb._4tgw8._njrw0

作为:

driver.find_elements_by_xpath(//span/button [text()="Approve"])

driver.find_elements_by_css_selector(._qv64e._gexxb._4tgw8._njrw0)

请参阅此链接http://selenium-python.readthedocs.io/locating-elements.html

答案 1 :(得分:0)

你可以尝试这个xpath:

driver.find_elements_by_xpath("//button[contains(text(),'Approve')]").click()