2017-07-16 82 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 

让我知道,如果这回答你的问题。