简体   繁体   English

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

[英]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/) 因此,我有一个进入Instagram.com的python文件,放入了帐户凭据,并在登录后最终将密钥输入了搜索框。将密钥放入搜索框后,我看不到提交密钥,因此Instagram可以将我带到帐户页面(例如:我在instagram搜索栏中输入了@streetgoodies,单击Enter,然后将我带到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!! searchbox.submit()引起了问题(我相信),谢谢!

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. 应该有WebDriverWait函数,因为登录页面不会很快出现。
  3. Main problem of your code and instagram is there isn't any submit button. 您的代码和instagram的主要问题是没有任何提交按钮。 So there should be send_keys(Keys.ENTER) 因此应该有send_keys(Keys.ENTER)
  4. One Keys.ENTER selects first item :) so I added another Keys.Enter 一个Keys.ENTER选择第一项:),所以我添加了另一个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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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