简体   繁体   English

如何使用python和selenium将文本发送给具有隐藏的type属性的输入元素

[英]How to send text to an input element with type attribute as hidden using python and selenium

I'm new to python and selenium. 我是python和selenium的新手。 I want to click on the get_likes_button and I need to send the value = 1803345990687013485 when doing that. 我想单击get_likes_button并且在执行此操作时需要发送值= 1803345990687013485。


Here's the HTML 这是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>

Here's the code 这是代码

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

I get the following message 我收到以下消息

Exception: Message: Element not visible. 异常:消息:元素不可见。

Try below snippet. 请尝试以下代码段。 Hope this will help you. 希望这会帮助你。

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

For clicking on Get Likes button you can use this code : 对于单击“喜欢”按钮,您可以使用以下代码:

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

After that if input type changed from type='hidden' , you can interact with input field as : 之后,如果输入类型从type='hidden'更改,则可以与输入字段进行交互,如下所示:

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

It's quite strange to see HTML like this : 看到这样的HTML很奇怪:

<input name="id">  

By the way, hope this will help. 顺便说一句,希望这会有所帮助。

This error message... 此错误消息...

Exception: Message: Element not visible.

...implies that the desired element is not visible . ...表示所需的元素不可见

The main issue is the <input> tag is having an attribute type="hidden" . 主要问题是<input>标记具有属性type="hidden"

To send the character sequence 1803345990687013485 to the input field and invoke click() on the button you can use the following solution: 要将字符序列 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Selenium Python 将文本发送到网页中的输入元素 - How to send text to an input element within a webpage using Selenium Python 如何在不使用 Python Selenium 中的发送键的情况下将文本输入到输入元素中? - How can you input text into an input element without using send keys in Python Selenium? Python Selenium:将密钥发送到标签“input type="hidden"” - Python Selenium: send keys to tag 'input type="hidden" ' 将文本发送到搜索框,这是一个带有 python selenium 的隐藏元素 - Send text to a search box which is a hidden element with python selenium 如何使用 Selenium Python 将键发送到 type = number 的元素? - How to send keys to an element with type = number using Selenium Python? 如何使用Python Selenium将文本发送到框架中的输入字段中 - How to send text into a input field within a frame using Python Selenium 如何使用 selenium 读取 python 中的隐藏文本? - how to read hidden text in python by using selenium? 如何在 python 中使用 selenium 滑动输入 [type=text]? - How can I slide input[type=text] using selenium in python? 如何在 Selenium 的输入元素中键入文本而不找到输入元素? - How to type text in input element in Selenium without finding the input element? 使用XPATH / Send_Keys更改元素属性-Selenium Python - Change Element Attribute Using XPATH / Send_Keys - Selenium Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM