[英]org.openqa.selenium.NoSuchElementException: Unable to locate element:- Search field
I am trying to search in learning search field in linkedin ( https://www.linkedin.com/learning/me?trk=nav_neptune_learning ) 我正在尝试在linkedin( https://www.linkedin.com/learning/me?trk=nav_neptune_learning )的学习搜索字段中进行搜索
Selenium with java: 硒与Java:
driver.findElement(By.xpath("//div[@class='search-container']/descendant::input[@type='text']")).sendKeys("Python");
Till step 3, it's working, search field is highlighted. 到第3步为止,它一直有效,搜索字段突出显示。
The final step always fails, with below exception 最后的步骤总是失败,以下例外
org.openqa.selenium.NoSuchElementException: Unable to locate element: org.openqa.selenium.NoSuchElementException:无法找到元素:
I tried the below xpath: 我尝试了以下xpath:
//div[@class='container global-nav__container']//div[@class='search-container']/artdeco-typeahead[@id='ember863']/div/input[@type='text']
//div[@class='search-container']/descendant::input[@type='text']
//div[@class='search-container']/artdeco-typeahead[@id='ember863']/div/input[@type='text']
//div[@class='search-container']/artdeco-typeahead/div/input[@type='text']
Interesting thing is with all the above xpath, I am able to find the element in the broswer, but the same xapth doesn't work in selenium code. 上面所有的xpath都有意思,我可以在浏览器中找到该元素,但是相同的xapth在硒代码中不起作用。
My code -Step 4 我的代码-步骤4
@Test
public static void search() throws InterruptedException, AWTException
{
Robot robot = new Robot();
for ( int i=0; i<=5;i++)
{
robot.keyPress(KeyEvent.VK_TAB);
Thread.sleep(2000);
}
driver.findElement(By.xpath("//input[@type='text']")).sendKeys("Python");
}
NoSuchElementException
happens generally in 2 scenarios. NoSuchElementException
通常在两种情况下发生。
You are getting the "NoSuchElementException" because your are entering the text even before the webelement on the page has completely loaded. 之所以得到“ NoSuchElementException”,是因为您甚至在页面上的网络元素完全加载之前就输入了文本。
I would suggest you wait till the webelement that you are looking for to get loaded first and then use sendkeys. 我建议您等到要查找的Web元素首先加载后再使用sendkeys。 Use can use WebDriverWait
class 使用可以使用WebDriverWait
类
so the refactored code should look somewhat like this. 因此,重构后的代码应该看起来像这样。
//Initializing the 'wait' with a 30 seconds deplay before it throws a NoSuchElementException
WebDriverWait wait = new WebDriverWait(driver,30);
wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("input[placeholder='Search for skills, subjects or software']"));
driver.findElement(By.cssSelector("input[placeholder='Search for skills, subjects or software']")).sendKeys("Python");
下面的代码将为您工作:
driver.findElement(By.cssSelector("input[placeholder='Search for skills, subjects or software']")).sendKeys("Python");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.