Webdriver-无法定位元素(Java)
我是Selenium和WebDriver方面的新手。 我有这个HTML:Webdriver-无法定位元素(Java),java,selenium,Java,Selenium,我是Selenium和WebDriver方面的新手。 我有这个HTML: 试试这个: WebDriverWait wait = new WebDriverWait(driver, 10); WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("input[data-test='testing-job-subject']"))); 试试这个: WebDriverWait w
试试这个:
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("input[data-test='testing-job-subject']")));
试试这个:
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("input[data-test='testing-job-subject']")));
如果您得到的异常是
NoTouchElementException
,可能有以下原因:-
- 可能是在您要查找元素时,它不会出现在
上,因此您应该实现DOM
以等待元素显示,如下所示:-WebDriverWait
WebDriverWait wait = new WebDriverWait(driver, 10); WebElement el = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input[data-test='testing-job-subject']")));
WebDriverWait wait = new WebDriverWait(driver, 10); //Find frame or iframe and switch wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("your frame id or name")); //Now find the element WebElement el = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input[data-test='testing-job-subject']"))); //Once all your stuff done with this frame need to switch back to default driver.switchTo().defaultContent();
- 此元素可能位于任何
或帧
内。如果是,您需要在查找元素之前切换该iframe
或frame
,如下所示:-iframe
WebDriverWait wait = new WebDriverWait(driver, 10); WebElement el = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input[data-test='testing-job-subject']")));
WebDriverWait wait = new WebDriverWait(driver, 10); //Find frame or iframe and switch wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("your frame id or name")); //Now find the element WebElement el = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input[data-test='testing-job-subject']"))); //Once all your stuff done with this frame need to switch back to default driver.switchTo().defaultContent();
NoTouchElementException
,可能有以下原因:-
- 可能是在您要查找元素时,它不会出现在
上,因此您应该实现DOM
以等待元素显示,如下所示:-WebDriverWait
WebDriverWait wait = new WebDriverWait(driver, 10); WebElement el = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input[data-test='testing-job-subject']")));
WebDriverWait wait = new WebDriverWait(driver, 10); //Find frame or iframe and switch wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("your frame id or name")); //Now find the element WebElement el = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input[data-test='testing-job-subject']"))); //Once all your stuff done with this frame need to switch back to default driver.switchTo().defaultContent();
- 此元素可能位于任何
或帧
内。如果是,您需要在查找元素之前切换该iframe
或frame
,如下所示:-iframe
WebDriverWait wait = new WebDriverWait(driver, 10); WebElement el = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input[data-test='testing-job-subject']")));
WebDriverWait wait = new WebDriverWait(driver, 10); //Find frame or iframe and switch wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("your frame id or name")); //Now find the element WebElement el = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input[data-test='testing-job-subject']"))); //Once all your stuff done with this frame need to switch back to default driver.switchTo().defaultContent();
driver.findElement(By.cssSelector("id*='Jobsubject'"));
这应该对你有用
driver.findElement(By.cssSelector("id*='Jobsubject'"));
我尝试了第一个原因,但仍然没有,只是另一个错误:“org.openqa.selenium.TimeoutException:预期条件失败:等待by.cssSelector定位的元素的可见性:输入[data test='testing-job-subject'](尝试10秒,间隔500毫秒)没有frame或iframe:/那么如何确保没有frame或iframe???@Mephy您确定在这里为link元素提供了正确的HTML代码??我尝试了第一个原因,但仍然没有,只是另一个错误:“org.openqa.selenium.TimeoutException:预期条件失败:等待by.cssSelector定位的元素的可见性:输入[data test='testing-job-subject'](尝试了10秒)以500毫秒为间隔)“并且没有帧或iframe::那么如何确保没有帧或iframe???@Mephy您确定您在此处为链接元素提供了正确的HTML代码??并确保此元素在页面上手动可见。。