【问题标题】:How to send text to an input element with type attribute as hidden using python and selenium如何使用python和selenium将文本发送到类型属性为隐藏的输入元素
【发布时间】:2018-06-17 07:42:21
【问题描述】:

我是 python 和 selenium 的新手。我想点击get_likes_button,这样做时我需要发送值 = 1803345990687013485。


这是 HTML

<form action="" method="post" accept-charset="utf-8"><span style="font-size: 14px;"> 
<i class="fa fa-heart" style="color: #F12938;"></i> 20 </span> 
<input type="hidden" value="1803345990687013485" name="id">
<button class="btn btn-primary pull-right" type="submit" name="submit"
 id="get_likes_button"> Get Likes </button> </form></b>

这是代码

driver.find_element_by_xpath("//input[@name='id']").send_key('1803345990687013485')
driver.find_element_by_id('get_likes_button').submit()

我收到以下消息

异常:消息:元素不可见。

【问题讨论】:

  • 你从哪里得到异常?请注意,第一个元素具有 type='hidden' 属性。另外,get_likes_buttonid,而不是name
  • 使用 findElementById() 因为 get_likes_button 是 id。
  • 我将其更改为 findElementById() 但我仍然收到相同的错误消息

标签: python selenium selenium-webdriver webdriver hidden


【解决方案1】:

此错误消息...

Exception: Message: Element not visible.

...暗示所需元素不可见

主要问题是&lt;input&gt; 标签具有type="hidden" 属性。

要将字符序列 1803345990687013485发送到输入字段并在按钮上调用click(),您可以使用以下解决方案:

driver.execute_script("document.getElementsByName('id')[0].setAttribute('type','text')")
driver.find_element_by_xpath("//input[@name='id']").send_key('1803345990687013485')
driver.find_element_by_xpath("//button[@class='btn btn-primary pull-right' and @id='get_likes_button']").click()

【讨论】:

    【解决方案2】:

    要点击 Get Likes 按钮,您可以使用以下代码:

    get_likes = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.ID, "get_likes_button")))  
    

    之后,如果输入类型从type='hidden' 更改,您可以与输入字段交互:

    input_field = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.NAME, "id")))  
    

    看到这样的HTML很奇怪:

    <input name="id">  
    

    顺便说一句,希望这会有所帮助。

    【讨论】:

      【解决方案3】:

      试试下面的sn-p。希望这会对你有所帮助。

      WebDriver driver = new FirefoxDriver();
      driver.navigate().to(URL);                    
      JavascriptExecutor javascriptExecuter = (JavascriptExecutor)driver;
      javascriptExecuter.executeScript("document.getElementsByName('id')[0].value='452525252525';");
      driver.findElement(By.id("get_likes_button")).submit();
      

      【讨论】:

      • 以上代码用java编写。使用 JavascriptExecutor 更新隐藏字段值,如上。
      猜你喜欢
      • 2021-07-03
      • 2022-12-14
      • 1970-01-01
      • 2014-05-31
      • 1970-01-01
      • 2011-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多