Selenium python input hidden

Python Selenium Calling Hidden File Input

I am using selenium-python with chromedriver. In a webpage,which is https://web.whatsapp.com/ ,I am trying to send a file with send_keys() to a file type input. But the problem is,that input element is only visible in inspector when you drop a file on the webpage.If you close that menu(I don’t mean native menu) element disappears from inspector. How can I call that element if element is not displayed? And element seems like this:

xpath='//*[@id="app"]/div/div[3]/div[1]/span[2]/span/div/div[2]/input' 
driver.execute_script("document.getElementById('parent_id').style.display='block';") driver.execute_script("document.getElementByXpath('Xpath').sendKeys('file');") 

1 Answer 1

If you wat to work with file input element then try to make it visible instead of it’s parent element as below :

xpath = '//*[@id="app"]/div/div[3]/div[1]/span[2]/span/div/div[2]/input' file = driver.find_element_by_xpath(xpath) file = driver.execute_script("arguments[0].style.display='block'; return arguments[0];", file) file.seng_keys("file") 

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.27.43548

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

Python Selenium: send keys to tag ‘input type=»hidden» ‘

I try to log in to this web page with my credentials. https://www.oddsportal.com/login I am able to get the «username» and «password» input boxes but I am not able to send the data. Selenium locates the elements (via «id» or otherwise), but gives problems when trying to send values or pressing enter: «AttributeError: ‘NoneType’ object has no attribute ‘click'» «AttributeError: ‘NoneType’ object has no attribute ‘send_keys'» I’ve tried find, xpath, actionchain and execute_script, too. Any clue how to click and send keys? TVM. I add some code tried:

us = driver.find_element_by_xpath('//input[@type="hidden"]') print(us) 
us = driver.find_element(By.ID, "login-username-sign") print(us) 
us = driver.find_element(By.ID, "login-username-sign") us.send_keys("1234") 
ElementNotInteractableException: Message: element not interactable (Session info: chrome=109.0.5414.74) 

Etc. «Info about human login»: To log in manually you have to put the cursor over the text box, click and then type. This actions fail (click and type) under Selenium (I dont get it clicks but I think Selinium does get the object). HTML:

Читайте также:  Plotly express python scatter

Showing us the code as you have implemented it will allow people to help you much more than showing the webpage you are trying to authenticate to.

I’m sorry for the paucity of initial information. I have expanded with more relevant information. Thanks.

4 Answers 4

You were close enough. However the locator strategy you were trying to use, i.e.

us = driver.find_element(By.ID, "login-username-sign") 

identifies two (2) elements within the HTML DOM. Among them first one is hidden where as the second matching element is your desired element.

login-username-sign

Solution

To cut sort the task of identifying the Username & Password field you can click on the LOGIN element in the top right corner of the page which opens the Username & Password fields within a Modal Dialog Box and you can identify the desired elements using the following locator strategies:

from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC driver.get('https://www.oddsportal.com/login') WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#onetrust-accept-btn-handler"))).click() WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='flex gap-3']//div[contains(@class, 'loginModalBtn') and starts-with(., 'Login')]"))).click() WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div#loginDiv form[action='https://www.oddsportal.com/userLogin'] input#login-username-sign"))).send_keys("daniel2014") driver.find_element(By.CSS_SELECTOR, "div#loginDiv form[action='https://www.oddsportal.com/userLogin'] input#login-password-sign-m").send_keys("daniel2014") 

modal_login

I can’t open that link due to security policy of my working computer, but I can be quite sure that your problem is caused by bad locators.

«AttributeError: ‘NoneType’ object has no attribute ‘click'»

«AttributeError: ‘NoneType’ object has no attribute ‘send_keys'»

Means you did not actually get the web element you trying to click / send keys to.
Also sending keys (text) with Selenium to element with ‘input type=»hidden» ‘ is also not possible. Selenium imitates human user GUI actions. As a human user you can not insert text to hidden element.

  1. First of all, you have to register the website creating an account manually by filling blank form using Username, password, email and country name from the following url
  2. Then go with your desired login url and run the selenium code and see the real scenerio from the selenium automated browser. Now you willnotice that the login url will ask to fillthe form with the username and password only and you have to use your registered username and password and click on login button. That okey! Nope. At first, accept cookies, then fill up the login form.
  3. You have to apply JavaScript execution in order to click on Login button, Otherwise, it will throw ClickIntercept Exception. Because Login button interacts with other dynamic element.
from selenium import webdriver from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By from selenium.webdriver.support.wait import WebDriverWait from webdriver_manager.chrome import ChromeDriverManager import time options = webdriver.ChromeOptions() options.add_argument("start-maximized") options.add_experimental_option("detach", True) driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()),options=options) URL ='https://www.oddsportal.com/login' driver.get(URL) time.sleep(5) cookie_btn = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, '(//*[@id="onetrust-button-group"]/button)[1]'))).click() UserName = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '(//*[@id="login-username-sign"])[2]'))).send_keys('Akij')#Your registered account's username PassWord = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, '#login-password-sign'))).send_keys('789456rtU') #Your registered account's password LogIn = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '(//*[@name="login-submit"])[2]'))) #You have to apply JavaSript execution in order to click on Login button, Otherwise it will throw ClickIntercept Exception #Because Login button interacts with other dynamic element. LogIn_click = driver.execute_script("arguments[0].click();", LogIn) time.sleep(1) 

See the below output:

Читайте также:  Https sdo urgaps ru login index php

Источник

Selenium (Python): How to insert value on a hidden input?

I’m using Selenium’s WebDriver and coding in Python. There’s a hidden input field in which I’m trying to insert a specific date value. The field originally produces a calendar, from which a user can select an appropriate date, but that seems like a more complicated endeavour to emulate than inserting the appropriate date value directly. The page’s source code looks like this:

where value=»2013-11-26″ is the field I’m trying to inject a value (it’s originally empty, ie: value=»» . I understand that WebDriver is not able to insert a value into a hidden input, because regular users would not be able to do that in a browser, but a workaround is to use Javascript. Unfortunately that’s a language I’m not familiar with. Would anyone know what would work?

1 Answer 1

from selenium import webdriver driver = webdriver.Firefox() driver.get('http://jsfiddle.net/falsetru/mLGnB/show/') elem = driver.find_element_by_css_selector('div.dijitReset>input[type=hidden]') driver.execute_script(''' var elem = arguments[0]; var value = arguments[1]; elem.value = value; ''', elem, '2013-11-26') 
from selenium import webdriver driver = webdriver.Firefox() driver.get('http://matrix.itasoftware.com/') elem = driver.find_element_by_xpath( './/input[@id="ita_form_date_DateTextBox_0"]' '/following-sibling::input[@type="hidden"]') value = driver.execute_script('return arguments[0].value;', elem) print("Before update, hidden input value = <>".format(value)) driver.execute_script(''' var elem = arguments[0]; var value = arguments[1]; elem.value = value; ''', elem, '2013-11-26') value = driver.execute_script('return arguments[0].value;', elem) print("After update, hidden input value = <>".format(value)) 

Источник

Write value to hidden element with selenium python script

I’m trying to write to a text box with my python selenium code but get an error since a parent tag of the text box is hidden.

driver.find_element_by_xpath("//input[@itemcode='XYZ']").send_keys(1) 

I see a Javascript executor workaround with java but need help with something similar for python script. Thanks in advance!!

1 Answer 1

Try this workaround(tested in Firefox and Chrome):

from selenium import webdriver from selenium.common.exceptions import NoSuchElementException browser = webdriver.Firefox() # Get local session(use webdriver.Chrome() for chrome) browser.get("http://www.example.com") # load page from some url assert "example" in browser.title # assume example.com has string "example" in title try: # temporarily make parent(assuming its id is parent_id) visible browser.execute_script("document.getElementById('parent_id').style.display='block'") # now the following code won't raise ElementNotVisibleException any more browser.find_element_by_xpath("//input[@itemcode='XYZ']").send_keys(1) # hide the parent again browser.execute_script("document.getElementById('parent_id').style.display='none'") except NoSuchElementException: assert 0, "can't find input with XYZ itemcode" 

Another workaround is even simpler(assuming the text box’s id is «XYZ», otherwise use any JS code that can retrieve it) and probably better if you only want to change the text box’s value:

browser.execute_script("document.getElementById('XYZ').value+='1'") 

Источник

Оцените статью