【问题标题】:Not able to locate text field in a webpage无法在网页中找到文本字段
【发布时间】:2021-07-05 21:32:03
【问题描述】:

我已经创建了代码

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

谁能指导我们如何输入值并按回车键?

【问题讨论】:

  • 您是否在等待页面加载?
  • 是的。使用 driver.implicitly_wait(10)
  • @sam 更好地使用显式等待特定元素
  • @same 我的意思是在您更新的代码中将驱动程序剪断 = chrome() 2 次​​span>
  • 你们还有问题>

标签: python python-3.x python-2.7 selenium


【解决方案1】:

使用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)

【讨论】:

  • 不,即使在 inputElement.submit() 之后也没有提交
  • 导入硒等待。检查更新的答案
  • @vitaliis 我建议替换 self -> webelement,不清楚它是 webelement 类的包装器
  • 无法替代)此方法来自源代码)。
  • @vitaliis 我认为这是你自己的包装器)你在 --headless 模式下试过了吗?好像不想搜索
【解决方案2】:

这已经是可行的解决方案,但适用于 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?

【讨论】:

  • 不一定,但在 chrome 上试过,在 chrome 上也不能用。
  • @sam 在 chrome 中完美地为我工作,用断言更新,顺利通过。顺便说一句,我只留下了 .submit()
  • 看起来很有趣的是,它在你的 chrome 上工作但不是我的,让我在 safari 上测试
  • 没有错误,打开moneycontrol.com后没有任何反应。让我发布我更新的代码
  • options.add_argument('--ignore-certificate-errors') - 这是个好主意。该网站存在安全问题。
【解决方案3】:
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

【讨论】:

  • page_load_strategy 是个有趣的想法
  • 最后,这个策略奏效了,并且加载完美。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-19
  • 2019-03-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多