0

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 enter image description here

<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.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
李菲凡
  • 1
  • 1
  • Please check if there is an iframe present in the dom structure – Sameer Arora Jan 02 '20 at 06:26
  • @SameerArora I have already checked whether there is any of iframe... – 李菲凡 Jan 02 '20 at 07:44
  • @SameerArora `my_driver.switch_to_default_content()` `my_driver.switch_to_frame(0)` `my_driver.switch_to_frame(1)` `my_driver.switch_to_frame(2)` They all got the error message:no such frame. Although the first one didn't show up any error, it had no effect. ;( – 李菲凡 Jan 02 '20 at 07:58

1 Answers1

0

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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • It didn't work for me still... It popped out the error message which didn't have any further explanation. – 李菲凡 Jan 03 '20 at 02:13