Python + Selenium找不到要发送密钥的元素 [英] Python +Selenium can't find element to send key

查看:111
本文介绍了Python + Selenium找不到要发送密钥的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经处理了很多天了,不知道如何解决...

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

这就是我要通过硒获得的元素

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


他们都像这样弹出警告

===>消息:没有这样的元素:无法找到元素:{"method":"css选择器","selector":"[name =" QUICKSEARCH_STRING]"}(会话信息:chrome = 79.0.3945.88)


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)

这是我的代码:

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')
#############################################


所以我使用

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.

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

结果与ActionChains相同.

我真的很感谢有人告诉我我的代码出了什么问题.

The result is as same as ActionChains.

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

推荐答案

由于您的用例是发送字符序列,因此您不必使用 presence_of_element_located()诱导 element_to_be_clickable() 的WebDriverWait,并且您可以使用以下

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:

  • 使用 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")

  • 使用 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
    

  • 这篇关于Python + Selenium找不到要发送密钥的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆