Selenium:它不会点击我需要的元素

时间:2021-01-27 19:42:37

标签: java selenium selenium-webdriver xpath css-selectors

我正在学习自动化,这是我的第二个脚本..我想做一些非常简单的事情,转到 https://demoqa.com/ 点击小部件,然后点击滑块,但我尝试了所有元素,但它不起作用......我不能尝试 Select,因为我有一个 ul il 但没有价值。 我也试过用一个 invisibily 的页脚元素,但它也不起作用..这是我的代码......(记住我在这方面真的很新......)我把我尝试过的所有方法都作为评论留下了

package paginas;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.pagefactory.AjaxElementLocatorFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Slider {
        //Identify all elements to use

        //Poblem: Unable to locate the element
        //@FindBy(className="btn btn-light active")

        //Problem: element not interactable
        //@FindBy(id="item-3")  
        
        //Problem (abs xpath):element click intercepted: Element <li class="btn btn-light " id="item-3">...</li> 
                //is not clickable at point (177, 621). Other element would receive the click: <footer>...</footer> 
        //@FindBy(xpath="/html[1]/body[1]/div[1]/div[1]/div[1]/div[2]/div[1]/div[1]/div[1]/div[4]/div[1]/ul[1]/li[4]")
        
        //Problem (Rel xpath: element click intercepted: Element <li class="btn btn-light " id="item-3">...</li> 
           //is not clickable at point (177, 621). Other element would receive the click: <footer>...</footer>)
        @FindBy(xpath="//div[4]//div[1]//ul[1]//li[4]")
        WebElement slider;      
        WebDriver driver;
                
        public Slider(WebDriver driver) {
            this.driver = driver;
            //Inicializacion de los elementos con una espera impicita
            PageFactory.initElements(new AjaxElementLocatorFactory(driver,20), this);
    
        }       
        public void clickonSlider()
        {   // 4 | click | css=.show #item-3 > .text | 
            // Hace click en Sliders
//          //driver.findElement(By.cssSelector(".show #item-3 > .text")).click();
            slider.click();
        }       
    
    
} 

Slider

1 个答案:

答案 0 :(得分:1)

元素尚不可见,这就是您收到 not interactable 错误的原因,而且 className 方法与多个类不匹配。

解决方案

通过 xpath 查找元素并在您可以单击它后使用 JavascriptExecutor 组件滚动到该元素。

WebElement slider = driver.findElement(By.xpath("//span[text()=\"Slider\"]"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", slider);
slider.click();

你也可以用@FindBy注解切换局部声明

@FindBy(xpath="//span[text()=\"Slider\"]"
WebElement slider;
相关问题