我是 python 和 selenium 的新手,真的很喜欢学习。
只是将我的脚趾浸入 selenium 中,但在让这个脚本运行时遇到了一些麻烦。
它没有给出错误,它只是打开浏览器而不执行任何操作。
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()
请您参考如下方法:
当您使用 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
