Python +Selenium can't find element to send key

被刻印的时光 ゝ 提交于 2021-02-11 13:54:40

问题


I already deal with it for many days having no idea how to solve it...

That is the element I want to get by selenium

<input name="QUICKSEARCH_STRING" id="QUICKSEARCH_STRING" onfocus="setTimeout('focusSearchElem()', 100);" type="text" value="">


They all pop out the warning like this one

===> Message: no such element: Unable to locate element: {"method":"css selector","selector":"[name="QUICKSEARCH_STRING"]"} (Session info: chrome=79.0.3945.88)

It is my code:

import selenium.webdriver
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains

my_driver = selenium.webdriver.Chrome()
account_box=my_driver.find_element_by_id('j_username')
account_box.send_keys('my_user_name')

#### I tried many ways to get the element####  

#way 1(get the elemnt by full xpath):
my_driver.implicitly_wait(10)
PlmSearchBox=my_driver.find_element_by_xpath("/html/body/div[5]/div[3]/form/div/div[3]/input") #doesn't work
#

#way 2(get the element by name):
my_driver.implicitly_wait(10)
PlmSearchBox=my_driver.find_element_by_name('QUICKSEARCH_STRING') #doesn't work
#

#way 3(get the element by xpath):
my_driver.implicitly_wait(10)
PlmSearchBox=my_driver.find_element_by_xpath('//*[@id="QUICKSEARCH_STRING"]') #doesn't work
#

#way 4(get the element by id):
my_driver.implicitly_wait(10)
PlmSearchBox=my_driver.find_element_by_id('QUICKSEARCH_STRING') #doesn't work
#

#way 5(get the element by using explicit wait):
PlmSearchBox=wait.until(selenium.webdriver.support.expected_conditions.presence_of_element_located((By.ID, "QUICKSEARCH_STRING")))  #doesn't work
#

PlmSearchBox.send_keys('93-55520-300E')
#############################################


And after trying those ways and failed, I noticed that the cursor is blinking in the input box which I want to send the key to.

So I use

ActionChains

PlmSearchBox = ActionChains(my_driver)
PlmSearchBox.send_keys('93-55520-300E')
PlmSearchBox.perform()

There isn't any error message poping out, but the input box still remains blank.

switch_to.active_element

PlmSearchBox=my_driver.switch_to.active_element
PlmSearchBox.clear
PlmSearchBox.send_keys('93-55520-300E')

The result is as same as ActionChains.

I will really appreciate that someone tells me what's wrong with my code.


回答1:


As your usecase is to send a character sequence, so instead of using presence_of_element_located() you have to induce WebDriverWait for the element_to_be_clickable() and and you can use either of the following Locator Strategy:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#QUICKSEARCH_STRING[name='QUICKSEARCH_STRING'][onfocus*='focusSearchElem']"))).send_keys("93-55520-300E")
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='QUICKSEARCH_STRING' and @name='QUICKSEARCH_STRING'][contains(@onfocus, 'focusSearchElem']"))).send_keys("93-55520-300E")
    
  • Note : You have to add the following imports:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    


来源:https://stackoverflow.com/questions/59559059/python-selenium-cant-find-element-to-send-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!