2017-06-04 65 views
0

我有問題,當我試圖點擊按鈕使用硒與webdriver Phantonjs。我的代碼是這樣的:Selenium PhantomJS類內的單擊按鈕

from selenium import webdriver 

driver = webdriver.PhantomJS() 
driver.set_window_size(1120, 550) 
driver.get("https://www.instagram.com/nike/") 
dt = driver.find_elements_by_class_name("a_8imhp _glz1g") 

for i in dt: 
    i.click() 

driver.quit() 

和錯誤是:

File "test.py", line 8, in <module> 
    dt = driver.find_elements_by_class_name("a_8imhp _glz1g") 
    File "/usr/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 427, in find_elements_by_class_name 
    return self.find_elements(by=By.CLASS_NAME, value=name) 
    File "/usr/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 782, in find_elements 
    'value': value})['value'] 
    File "/usr/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 238, in execute 
    self.error_handler.check_response(response) 
    File "/usr/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 193, in check_response 
    raise exception_class(message, screen, stacktrace) 
selenium.common.exceptions.WebDriverException: Message: {"errorMessage":"Compound class names not permitted","request":{"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"103","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:41380","User-Agent":"Python-urllib/3.6"},"httpVersion":"1.1","method":"POST","post":"{\"using\": \"class name\", \"value\": \"a_8imhp _glz1g\", \"sessionId\": \"fba6d030-4934-11e7-8712-9506860019f4\"}","url":"/elements","urlParsed":{"anchor":"","query":"","file":"elements","directory":"/","path":"/elements","relative":"/elements","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/elements","queryKey":{},"chunks":["elements"]},"urlOriginal":"/session/fba6d030-4934-11e7-8712-9506860019f4/elements"}} 
Screenshot: available via screen 
+0

你能考慮更新我們的按鈕你想點擊? – DebanjanB

+0

我試着點擊「加載更多」,當你向下滾動時,你可以看到instagram上的按鈕。 –

回答

1

你不能傳遞一個以上的類名find_elements_by_class_name()。您可以嘗試傳遞單個名稱,例如

driver.find_elements_by_class_name("a_8imhp") 

或使用複雜的選擇器,例如, XPathCSS選擇

只需更換這

dt = driver.find_elements_by_class_name("a_8imhp _glz1g") 

與此

dt = driver.find_elements_by_css_selector(".a_8imhp._glz1g") 

或本

dt = driver.find_elements_by_xpath("//*[@class='a_8imhp _glz1g') 
+0

這是工作!非常感謝你andersson! –