我正在关注YouTube教程,并且已经复制了代码,但是仍然出现此错误。视频中的那个家伙正在使用Firefox,但我决定使用chrome。
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
class InstagramBot:
def __init__(self, username, password):
self.username = username
self.password = password
self.driver =webdriver.Chrome("/Users/Ruby/Downloads/chromedriver_win32/chromedriver.exe")
def CloseBrowser(self):
self.driver.close()
def login(self):
driver = self.driver
driver.get("https://www.instagram.com/")
time.sleep(5)
login_button = driver.find_element_by_xpath("//a[@href='/accounts/login/?source=auth_switcher']")
login_button.click()
time.sleep(5)
user_name_elem = driver.find_element_by_xpath("//input[@name='username']")
user_name_elem.clear()
user_name_elem.send_keys(self.username)
password_elem = driver.find_element_by_xpath("//input[@name='password']")
password_elem.clear()
password_elem.send_keys(self.password)
password_elem.send_keys(Keys.RETURN)
time.sleep(5)
def like_photo(self, hashtag):
driver = self.driver
driver.get("https://www.instagram.com/explore/tags/" + hashtag + "/")
time.sleep(5)
for i in range(1, 3):
driver.execute_script("window.scrollTo(@, document.body.scrollHeight);")
time.sleep(5)
hrefs = driver.find_element_by_tag_name('a')
pic_href = [elem.get_attribute('href') for elem in hrefs]
pic_href = [href for href in pic_href if hashtag in href]
print(hashtag + ' photos: ' +str(len(pic_href)))
myIg = InstagramBot("that_dominant_male_3.0", "Chhs1905")
myIg.login()
myIg.like_photo('newyork')
我收到此错误:
selenium.common.exceptions.WebDriverException:消息:未知错误:Runtime.evaluate抛出异常:SyntaxError:无效或意外的令牌 (会议信息:chrome = 71.0.3578.98) (驱动程序信息:chromedriver = 2.38.552522(437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform = Windows NT 6.1.7601 SP1 x86_64)