Python / Selenium - 无法在Instagram搜索输入中提交密钥

时间:2018-05-05 21:46:14

标签: python python-3.x selenium automation instagram

所以我有一个python文件进入Instagram.com,放入帐户凭据,最后在登录后将密钥输入搜索框。 将密钥放入搜索框后,我无法看到提交密钥,因此Instagram可以将我带到帐户页面 (例如:我将@streetgoodies放在Instagram搜索栏中,我点击回车,它将我带到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()导致问题(我相信) 谢谢!!

1 个答案:

答案 0 :(得分:0)

我为你写了一个剧本。我先解释一下:

  1. 我有直接登录页面。因此,您无需搜索登录信息。
  2. 应该有WebDriverWait功能,因为登录页面不会很快出现。
  3. 您的代码和Instagram的主要问题是没有任何提交按钮。所以应该有send_keys(Keys.ENTER)
  4. One Keys.ENTER选择第一项:)所以我添加了另一个Keys.Enter
  5. 此代码有效:

    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)