2017-07-16 81 views
1

我是新來的蟒蛇和硒真的很享受學習一點。Python +硒,打開瀏覽器,然後什麼都不做

只是把我的腳趾浸入硒,並有一些麻煩讓這個腳本工作。

它沒有給出錯誤,它只是打開瀏覽器,什麼都不做。

from selenium import webdriver 
from selenium.webdriver.common.keys import Keys 

kw = input("enter the keyword to search on google") 

# creates firefox session 
driver = webdriver.Firefox(executable_path=r'C:\Users\Web\Desktop\geckodriver-v0.18.0-win32\geckodriver.exe') 
driver.implicitly_wait(30) 


# navigate to google 
driver.get("http://www.google.com") 

#get the search textfield 
search_field = driver.find_element_by_id("lst-ib") 
search_field.clear() 

#enter search kw and submit 
search_field.send_keys(kw) 
search_field.submit() 

lists = driver.find_element_by_class_name("_Rm") 

print ("Found " + str(len(lists)) + "searches:") 

i=0 
for listitem in lists: 
    print(listitem) 
    i=i+1 
    if(i>10): 
     break 

driver.quit() 
+0

更新壁虎驅動程序和瀏覽器。 –

回答

0

這裏是回答你的問題:

當你與Selenium 3.4.3,geckodriver v0.17.0工作,Mozilla Firefox 53.0通過的Python 3.6.1;因爲您一直試圖從driver.find_element_by_class_name("_Rm")中創建一個列表並打印列表項,我假設您要打印的鏈接不是WebElements,例如, https://pypi.python.org/pypi/selenium等下面是一些簡單的調整,你可以用它來實現以下自己的代碼塊:

from selenium import webdriver 
from selenium.webdriver.common.keys import Keys 
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary 

binary = FirefoxBinary('C:\\Program Files\\Mozilla Firefox\\firefox.exe') 
kw = input("Provide the keyword to search through google : ") 

# creates firefox session 
driver = webdriver.Firefox(firefox_binary=binary, executable_path="C:\\Utility\\BrowserDrivers\\geckodriver.exe") 
driver.implicitly_wait(30) 


# navigate to google 
driver.get("http://www.google.com") 

#get the search textfield 
search_field = driver.find_element_by_id("lst-ib") 
search_field.clear() 

#enter search kw and submit 
search_field.send_keys(kw) 
search_field.submit() 

lists = driver.find_elements_by_class_name("_Rm") 
print ("Elements found : {}".format(len(lists))) 

i=0 
print("Here are the links : ") 
for listitem in lists: 
    print(listitem.get_attribute("innerHTML")) 
    i=i+1 
    if(i>10): 
    break 

driver.quit() 

在控制檯上的輸出如下:

Provide the keyword to search through google : Selenium 
Elements found : 11 
Here are the links : 
https://en.wikipedia.org/wiki/Selenium_(software) 
www.seleniumhq.org/ 
www.guru99.com/selenium-tutorial.html 
https://en.wikipedia.org/wiki/Selenium_(software) 
toolsqa.com/selenium-tutorial/ 
selenium-python.readthedocs.io/ 
selenium-python.readthedocs.io/getting-started.html 
https://github.com/SeleniumHQ/selenium 
https://github.com/SeleniumHQ 
https://www.pluralsight.com/courses/selenium 
www.scalatest.org/user_guide/using_selenium 

讓我知道,如果這回答你的問題。