简体   繁体   中英

Python / Selenium - Can't submit the keys in Instagram search input

So I have a python file that enters Instagram.com, puts the account credentials, and finally enters the keys into the search box after logging in. Once it puts the keys into the search box, I can't see to submit the keys so Instagram can take me to the account page (ex: I put @streetgoodies in the instagram search bar, i click enter, and it brings me to www.instagram.com/streetgoodies/)

Is there any way i can submit the keys into the search so it can redirect me to the search query that i have requested?

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()

The searchbox.submit() is causing the issue (I believe) Thank you!!

I wrote down a script for you. Let me explain first:

  1. I got direct login page. So, you do not need to search for login.
  2. There should be WebDriverWait function because login page does not appear quickly.
  3. Main problem of your code and instagram is there isn't any submit button. So there should be send_keys(Keys.ENTER)
  4. One Keys.ENTER selects first item :) so I added another Keys.Enter

This code works:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM