I want to automate logging and put user name in hidden field and I have the following simple code:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox(executable_path='C:\\Users\\user\\Desktop\\geckodriver.exe')
driver.get("https://timeoff.guru/to/company/timeoff.nsf")
driver.implicitly_wait(10)
driver.find_element(By.XPATH, '//*[@id="username"]').send_keys('user')
driver.find_element(By.XPATH, '//*[@id="password"]').send_keys('pass', Keys.ENTER)
driver.find_element(By.XPATH, '//*[@id="select2-searchMember-container"]').click()
driver.find_element(By.XPATH, '/html/body/span/span/span[1]/input').send_keys('user', Keys.ENTER)
Everything works good except last Keys.Enter which doesn't execute.
CodePudding user response:
Selenium was designed to work as a user would which means that it can't (without workarounds) operate on hidden elements. The workaround in this case would be to use JavaScript.
NOTE: This is not advised because you are manipulating the page outside of what a normal user would be able to do and are bypassing any number of things like validations, etc. that the page has in place to avoid bad inputs, etc.
But... without the actual page link, we can't advise you on the proper way to do this.
Using JS, your last line would be replaced with the lines below
input = driver.find_element(By.XPATH, '/html/body/span/span/span[1]/input')
driver.execute_script("arguments[0].value='user'", input)
// now you need to click on Submit or whatever the button is to proceed
CodePudding user response:
I resolve my issue. Here's the code:
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox(executable_path='D:\selenium\geckodriver.exe')
driver.get("https://timeoff.guru/to/company/timeoff.nsf")
driver.implicitly_wait(10)
driver.find_element(By.XPATH, '//*[@id="username"]').send_keys('user')
driver.find_element(By.XPATH, '//*[@id="password"]').send_keys('pass', Keys.ENTER)
driver.find_element(By.XPATH, '/html/body/div[2]/div[11]/div[1]/div[6]/div[1]/div[2]/span/span[1]/span/span[2]').click()
elem = driver.find_element(By.XPATH, '/html/body/span/span/span[1]/input')
elem.send_keys('user')
sleep(5)
elem.send_keys(Keys.ENTER)