因此,我正在尝试创建insta机器人,该机器人会在搜索栏中打开特定的#标签,然后导航至该标签。我在将机器人导航到搜索栏时遇到问题,它总是告诉我找不到路径,有什么主意我该如何使机器人成为目标搜索栏并向其发送主题标签键?这是我的代码:
from selenium import webdriver
from time import sleep
from insta import username,password,hashtag
class InstaBot():
def __init__(self):
self.driver = webdriver.Chrome()
def login(self):
self.driver.get('https://www.instagram.com/')
sleep(3)
#loggin in to instagram with facebook
fb_btn = self.driver.find_element_by_xpath('//*[@id="react-root"]/section/main/article/div[2]/div[1]/div/form/div[1]/button')
fb_btn.click()
#logging into acc
email = self.driver.find_element_by_xpath('//*[@id="email"]')
email.send_keys(username)
pswd = self.driver.find_element_by_xpath('//*[@id="pass"]')
pswd.send_keys(password)
login_btn = self.driver.find_element_by_xpath('//*[@id="loginbutton"]')
login_btn.click()
sleep(4)
self.driver.find_element_by_xpath("//button[contains(text(), 'Not Now')]")\
.click()
#navigating to search bar and sending hashtag into it
hashtag = self.driver.find_element_by_xpath('//*[@id="react-root"]/section/nav/div[2]/div/div/div[3]/div/div[1]/a/svg')
hashtag.send_keys(hashtag)
您可以通过执行JavaScript导航到搜索栏:
SCRIPT = f"document.getElementsByClassName('XTCLo x3qfX')[0].value = '{SEARCH_VALUE}'"
driver.execute_script(SCRIPT)
但是,如果您只想转到主题标签页面,我建议您使用driver.get(f"https://www.instagram.com/explore/tags/{HASHTAG}/")