Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Webdriver-无法定位元素(Java)_Java_Selenium - Fatal编程技术网

Webdriver-无法定位元素(Java)

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

我是Selenium和WebDriver方面的新手。 我有这个HTML:

试试这个:

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代码??并确保此元素在页面上手动可见。。