我的程序在使用Selenium从网页上获取现有类时遇到问题。我的WebDriver.execute_script函数似乎无法正常工作。
import urllib
from selenium import webdriver
#Path to the chromedriver is definitely working fine.
path_to_chromedriver = 'C:\Users\Ben\Desktop\Coding\FreeFoodFinder\chromedriver.exe'
browser = webdriver.Chrome(executable_path = path_to_chromedriver)
url = 'http://www.maidservicetexas.com/'
browser.implicitly_wait(30)
browser.get(url)
content = browser.execute_script("document.getElementsByClassName('content')");
#Just printing the first character of the returned content's toString for now. Don't want the whole thing yet.
#Only ever prints 'N', the first letter of 'None'...so obviously it isn't finding the jsgenerated content even after waiting.
print content
我的程序返回'无,'这告诉我javascript函数没有返回值/正在执行。 Chrome的网络开发工具告诉我内容'肯定是一个有效的类名。网页甚至没有动态生成(我的最终目标是抓取动态内容,这就是我在运行脚本之前让我的WebDriver等待30秒的原因。)
答案 0 :(得分:2)
返回值:
content = browser.execute_script("return document.getElementsByClassName('content');");