[英]Can't get instagram search box using selenium and python
I have been building an instagram bot using python and selenium.我一直在使用 python 和 selenium 构建一个 instagram 机器人。 I am trying to get the search box element and run a search.
我正在尝试获取搜索框元素并运行搜索。 However selenium can't seem to be able to detect the element.
然而,硒似乎无法检测到该元素。 I have tried getting it by every method available (Xpath, css selector, etc).
我已经尝试通过每种可用的方法(Xpath、css 选择器等)来获取它。
Here is one of the ways I've tried searching for it (please note that I have also implemented time.sleep to give the page time to load).这是我尝试搜索它的方法之一(请注意,我还实现了 time.sleep 以提供页面加载时间)。
def search():
DRIVER_PATH = '/Users/kiluas/selenium/driver/chromedriver'
webd = webdriver.Chrome(executable_path = DRIVER_PATH)
webd.find_element_by_css_selector('#react-root > section > nav > div._8MQSO.Cx7Bp > div > div > div.LWmhU._0aCwM > input')
Here is the error log it spits out:这是它吐出的错误日志:
Traceback (most recent call last):
File "autologinIg.py", line 80, in <module>
loginInstagram('username', 'password', 3)
File "autologinIg.py", line 45, in loginInstagram
search()
File "autologinIg.py", line 78, in search
webd.find_element_by_css_selector('#react-root > section > nav > div._8MQSO.Cx7Bp > div > div > div.LWmhU._0aCwM > input')
File "/usr/local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 598, in find_element_by_css_selector
return self.find_element(by=By.CSS_SELECTOR, value=css_selector)
File "/usr/local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 976, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "/usr/local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"#react-root > section > nav > div._8MQSO.Cx7Bp > div > div > div.LWmhU._0aCwM > input"}
(Session info: chrome=85.0.4183.121)
Any idea what is causing this behaviour?知道是什么导致了这种行为?
Sometimes the class names they vary in each run so that we cant automate Your selector is: #react-root > section > nav > div._8MQSO.Cx7Bp > div > div > div.LWmhU._0aCwM > input'有时它们在每次运行中的类名都不同,因此我们无法自动化您的选择器是:#react-root > section > nav > div._8MQSO.Cx7Bp > div > div > div.LWmhU._0aCwM > input'
div._8MQSO.Cx7Bp - Is this remaining constant in your css selector? div._8MQSO.Cx7Bp - 这在你的 css 选择器中是否保持不变?
I am able to insert value in searchbox
.我可以在
searchbox
插入值。
Induce WebDriverWait
() and wait for element_to_be_clickable
() and following xpath
诱导
WebDriverWait
() 并等待element_to_be_clickable
() 和跟随xpath
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//input[@placeholder='Search']"))).send_keys("text to be search")
You need to import below libraries.您需要导入以下库。
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Please note if you received any notification pop up you need to use below code before search.请注意,如果您收到任何弹出通知,您需要在搜索前使用以下代码。
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//button[text()='Not Now']"))).click()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.