gpt4 book ai didi

python - 在 Selenium 中编辑文本字段

转载 作者:太空宇宙 更新时间:2023-11-03 15:40:36 24 4
gpt4 key购买 nike

我正在尝试将一个值插入 Google 搜索字段。这是 IPython 中的代码:

    In [1]: from selenium import webdriver

In [2]: driver=webdriver.Chrome("/ChromeDriver/chromedriver")

In [3]: driver.get("https://www.google.com/")

In [4]: i=driver.find_elements_by_id("lst-ib")[0]

In [5]: i.click()

In [6]: i.sendKeys("test")
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-6-dcc9ebf0e928> in <module>()
----> 1 i.sendKeys("test")

AttributeError: 'WebElement' object has no attribute 'sendKeys'

In [7]: i.setAttribute("value","test")
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-7-cf9217d1c7f8> in <module>()
----> 1 i.setAttribute("value","test")

AttributeError: 'WebElement' object has no attribute 'setAttribute'

i 是 Google 搜索输入字段,我无法填写值。有人知道如何向该字段插入值吗?

最佳答案

sendKeys()Java 方法。您需要send_keys():

i.send_keys("test")

还要注意,Python 中没有 setAttribute() 这样的方法。您可能需要使用

driver.execute_script('arguments[0].setAttribute("value", "test")', i)

关于python - 在 Selenium 中编辑文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42175086/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com