无法在网页中找到文本字段
Not able to locate text field in a webpage
我已经创建了代码
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("https://www.moneycontrol.com")
wait = WebDriverWait(driver, 20)
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument('--ignore-certificate-errors')
driver.get("https://www.moneycontrol.com")
inputElement = WebDriverWait(driver, 30).until(
EC.visibility_of_element_located((By.XPATH, " (//input[@id='search_str'])[1]")))
inputElement.send_keys('IOC')
inputElement.submit()
assert driver.title == 'IOC Share Price, IOC Stock Price, Indian Oil Corporation Ltd. Stock Price, Share Price, Live BSE/NSE, Indian Oil Corporation Ltd. Bids Offers. Buy/Sell Indian Oil Corporation Ltd. news & tips, & F&O Quotes, NSE/BSE Forecast News and Live Quotes'
driver.quit()
任何人都可以指导我们如何输入值并按回车键吗?
使用CSS
选择器找到它。另外,添加等待:
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable(
(By.CSS_SELECTOR, '#form_topsearch>.txtsrchbox.FL')))
inputElement = driver.find_element_by_css_selector("#form_topsearch>.txtsrchbox.FL")
inputElement.send_keys("some input")
如果以上代码不起作用,请在 inputElement.send_keys()
之前添加 inputElement.click()
。
问题是 Selenium 不认为您的 search_str
id 是唯一的。
此外,我认为您不需要在您的情况下使用 inputElement.submit()
。搜索字段看起来不像一个表单。
来自 Selenium 源代码:
def submit(self):
"""Submits a form."""
if self._w3c:
form = self.find_element(By.XPATH, "./ancestor-or-self::form")
self._parent.execute_script(
"var e = arguments[0].ownerDocument.createEvent('Event');"
"e.initEvent('submit', true, true);"
"if (arguments[0].dispatchEvent(e)) { arguments[0].submit() }", form)
else:
self._execute(Command.SUBMIT_ELEMENT)
这里已经有可行的解决方案,但是 Chrome:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
options = Options()
options.add_argument('--ignore-certificate-errors')
driver = webdriver.Chrome(options=options)
driver.get("https://www.moneycontrol.com")
inputElement = WebDriverWait(driver, 30).until(
EC.visibility_of_element_located((By.XPATH, "(//input[@id='search_str'])[1]")))
inputElement.send_keys('IOC')
inputElement.submit()
assert driver.title == 'IOC Share Price, IOC Stock Price, Indian Oil Corporation Ltd. Stock Price, Share Price, Live BSE/NSE, Indian Oil Corporation Ltd. Bids Offers. Buy/Sell Indian Oil Corporation Ltd. news & tips, & F&O Quotes, NSE/BSE Forecast News and Live Quotes'
driver.quit()
是否需要 Firefox?
options = Options()
options.add_argument('--ignore-certificate-errors')
options.page_load_strategy = 'eager'
driver = webdriver.Chrome(ChromeDriverManager().install(),options=options)
wait = WebDriverWait(driver, 20)
driver.get("https://www.moneycontrol.com")
inputElement=wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#form_topsearch>.txtsrchbox.FL')))
inputElement.send_keys('IOC',Keys.ENTER)
您可以使用页面加载策略来停止长页面加载以单击输入,然后将 Keys.ENTER 发送到输入标签。当前使用
进入页面
information you’re about to submit is not secure Because this form is being submitted using a connection that’s not secure, your information will be visible to others.
然后使用
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#proceed-button'))).click()
哪个应该产生
导入
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
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
我已经创建了代码
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("https://www.moneycontrol.com")
wait = WebDriverWait(driver, 20)
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument('--ignore-certificate-errors')
driver.get("https://www.moneycontrol.com")
inputElement = WebDriverWait(driver, 30).until(
EC.visibility_of_element_located((By.XPATH, " (//input[@id='search_str'])[1]")))
inputElement.send_keys('IOC')
inputElement.submit()
assert driver.title == 'IOC Share Price, IOC Stock Price, Indian Oil Corporation Ltd. Stock Price, Share Price, Live BSE/NSE, Indian Oil Corporation Ltd. Bids Offers. Buy/Sell Indian Oil Corporation Ltd. news & tips, & F&O Quotes, NSE/BSE Forecast News and Live Quotes'
driver.quit()
任何人都可以指导我们如何输入值并按回车键吗?
使用CSS
选择器找到它。另外,添加等待:
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable(
(By.CSS_SELECTOR, '#form_topsearch>.txtsrchbox.FL')))
inputElement = driver.find_element_by_css_selector("#form_topsearch>.txtsrchbox.FL")
inputElement.send_keys("some input")
如果以上代码不起作用,请在 inputElement.send_keys()
之前添加 inputElement.click()
。
问题是 Selenium 不认为您的 search_str
id 是唯一的。
此外,我认为您不需要在您的情况下使用 inputElement.submit()
。搜索字段看起来不像一个表单。
来自 Selenium 源代码:
def submit(self):
"""Submits a form."""
if self._w3c:
form = self.find_element(By.XPATH, "./ancestor-or-self::form")
self._parent.execute_script(
"var e = arguments[0].ownerDocument.createEvent('Event');"
"e.initEvent('submit', true, true);"
"if (arguments[0].dispatchEvent(e)) { arguments[0].submit() }", form)
else:
self._execute(Command.SUBMIT_ELEMENT)
这里已经有可行的解决方案,但是 Chrome:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
options = Options()
options.add_argument('--ignore-certificate-errors')
driver = webdriver.Chrome(options=options)
driver.get("https://www.moneycontrol.com")
inputElement = WebDriverWait(driver, 30).until(
EC.visibility_of_element_located((By.XPATH, "(//input[@id='search_str'])[1]")))
inputElement.send_keys('IOC')
inputElement.submit()
assert driver.title == 'IOC Share Price, IOC Stock Price, Indian Oil Corporation Ltd. Stock Price, Share Price, Live BSE/NSE, Indian Oil Corporation Ltd. Bids Offers. Buy/Sell Indian Oil Corporation Ltd. news & tips, & F&O Quotes, NSE/BSE Forecast News and Live Quotes'
driver.quit()
是否需要 Firefox?
options = Options()
options.add_argument('--ignore-certificate-errors')
options.page_load_strategy = 'eager'
driver = webdriver.Chrome(ChromeDriverManager().install(),options=options)
wait = WebDriverWait(driver, 20)
driver.get("https://www.moneycontrol.com")
inputElement=wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#form_topsearch>.txtsrchbox.FL')))
inputElement.send_keys('IOC',Keys.ENTER)
您可以使用页面加载策略来停止长页面加载以单击输入,然后将 Keys.ENTER 发送到输入标签。当前使用
进入页面information you’re about to submit is not secure Because this form is being submitted using a connection that’s not secure, your information will be visible to others.
然后使用
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#proceed-button'))).click()
哪个应该产生
导入
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
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