问题描述
from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome(r"C:\Users\m\Desktop\python\selen\chromedriver.exe")
driver.set_page_load_timeout(10)
driver.get("https://google.com")
driver.find_element_by_name("q").send_keys("Automation")
driver.find_element_by_name("btnk").send_keys(keys.ENTER)
time.sleep(4)
driver.quit()
我正在尝试使用以下简单代码来打开google页面并搜索自动化"一词,但我得到了此结果,并且程序在此行停止运行:driver.find_element_by_name("btnk").send_keys(keys.ENTER)
I am trying this simple code to open google page and search "automation" word, but i get this result and the program stops running at this line:driver.find_element_by_name("btnk").send_keys(keys.ENTER)
Traceback (most recent call last):
File "C:\Users\m\Desktop\python\selen\auto.py", line 9, in <module>
driver.find_element_by_name("btnk").send_keys(keys.ENTER)
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 496, in find_element_by_name
return self.find_element(by=By.NAME, value=name)
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
'value': value})['value']
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"name","selector":"btnk"}
(Session info: chrome=74.0.3729.131)
(Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}),platform=Windows NT 10.0.17134 x86_64)
出什么问题了?
推荐答案
请检查"find_element_by_name"的搜索按钮或使用xpath.
Please check 'find_element_by_name' for search button or use xpath.
我在Java中运行脚本.希望这对您有帮助...
I run script in java.Hope this helps you...
要找到 xpath ,请使用 Selenium IDE .
,代码如下:-
WebDriver driver =new FirefoxDriver();
driver.get("https://www.google.com");
WebDriverWait wait = new WebDriverWait (driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@name='q']")));
WebElement ele = driver.findElement(By.xpath("//input[@name='q']"));
ele.sendKeys("q");
driver.findElement(By.xpath("(//input[@name='btnK'])[2]")).sendKeys(Keys.ENTER);
然后一个建议删除'thread.sleep',因为它会在提及时间停止程序.
And one suggestion drop 'thread.sleep' , because it stop program for mention time.
Thread.sleep():在睡眠代码中,即使工作页面在1秒钟后准备就绪,代码也会始终在括号内等待提及的秒数.因此,这可能会减慢测试速度.
Thread.sleep(): In sleep code will always wait for mentioned seconds in side the parentheses even in case working page is ready after 1 sec. So this can slow the tests.
显式等待:显式等待是一种等待特定条件发生然后再继续执行代码的等待.
Explicit wait: An explicit waits is a kind of wait for a certain condition to occur before proceeding further in the code.
隐式等待:隐式等待是告诉WebDriver在尝试查找一个或多个元素(如果它们不立即可用)时轮询DOM一定时间.默认设置为0
Implicit wait: An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0
是在进行硒测试时使用Thread.sleep是一个好习惯吗?
Is it good practise to use Thread.sleep while doing selenium Test?
已
也请参考此 https://stackoverflow.com/a/56078618/4513879 答案.
Element is not reachable by keyboard
用简单的词表示无法使用键盘访问该元素,这意味着您甚至都不会与之进行物理交互.请避免使用JS单击.用户无法单击不可见,被遮盖或不在屏幕上的元素,等等.Selenium旨在像用户一样操作,并在用户无法单击元素时引发错误.
Element is not reachable by keyboard
in plain words means that the element can’t be reached using the keyboard, which means you won't be physically interact with it even.avoid using JS clicks. A user can't click an element that isn't visible or is covered or is off the screen, etc. Selenium was designed to act like a user and throw errors when a user can't click an element.
有关更多信息,请通过 https://stackoverflow.com/a/49872160/4513879 此答案.
For more information please go through https://stackoverflow.com/a/49872160/4513879 this answer.
更新代码:-
WebDriver driver =new FirefoxDriver();
driver.get("https://www.google.com");
WebDriverWait wait = new WebDriverWait (driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@name='q']")));
WebElement ele = driver.findElement(By.xpath("//input[@name='q']"));
ele.sendKeys("q");
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.name("btnK"))).click();
在我添加的上述代码中的
new WebDriverWait(driver,20).until(ExpectedConditions.elementToBeClickable(By.name("btnK"))).click();
此行.
in above code I addednew WebDriverWait(driver,20).until(ExpectedConditions.elementToBeClickable(By.name("btnK"))).click();
this line.
这篇关于send_keys(Keys.ENTER)上的Selenium Webdriver错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!