所以我有一个python文件,输入Instagram.com,放入帐户凭据,最后在登录后将密钥输入到搜索框中。 将密钥放入搜索框中后,我看不到提交密钥,因此 Instagram 可以将我带到帐户页面 (例如:我将 @streetgoodies 放入 Instagram 搜索栏中,然后单击 Enter,它会将我带到 www.instagram.com/streetgoodies/)
有什么方法可以将密钥提交到搜索中,以便它可以将我重定向到我请求的搜索查询吗?
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# start a new browser session
browser = webdriver.Chrome('PATH/TO/DRIVER')
# navigate to a webpage
browser.get('https://www.instagram.com')
# find login link
login_elem = browser.find_element_by_xpath(
'//*[@id="react-root"]/section/main/article/div[2]/div[2]/p/a')
# click login in button
login_elem.click()
# send login info credentials to correct input boxes
browser.find_element_by_xpath("//input[@name='username']").send_keys('USERNAME')
browser.find_element_by_xpath("//input[@name='password']").send_keys('PASSWORD')
# click final login button
browser.find_element_by_xpath("//button[contains(.,'Log in')]").click()
# find hidden search bar
searchbox = WebDriverWait(browser, 10).until(
EC.visibility_of_element_located(
(By.XPATH, "//input[@placeholder='Search']")
)
)
# send search into input
searchbox.send_keys('streetgoodies')
searchbox.submit()
searchbox.submit() 导致了这个问题(我相信) 谢谢!!
我给你写了一个剧本。先解释一下:
此代码有效:
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# start a new browser session
browser = webdriver.Chrome('/pathtochromedriver')
# navigate to a webpage
browser.get('https://www.instagram.com/accounts/login/')
login_wait = WebDriverWait(browser, 10)
# click login in button
elem = login_wait.until(EC.visibility_of_element_located((By.XPATH, ".//input[@name='username']")))
elem.send_keys("usrname")
elem = login_wait.until(EC.visibility_of_element_located((By.XPATH, ".//input[@name='password']")))
elem.send_keys("passwd")
# click final login button
browser.find_element_by_xpath("//button[contains(.,'Log in')]").click()
# find hidden search bar
searchbox = WebDriverWait(browser, 10).until(
EC.visibility_of_element_located(
(By.XPATH, "//input[@placeholder='Search']")
)
)
# send search into input
searchbox.send_keys('streetgoodies')
time.sleep(2)
# searchbox.submit()
searchbox.send_keys(Keys.ENTER)
time.sleep(1)
searchbox.send_keys(Keys.ENTER)