【问题标题】:Selenium: It wont click on the element i needSelenium:它不会点击我需要的元素
【发布时间】:2021-05-01 16:59:04
【问题描述】:

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

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();
        }       
    
    
} 

【问题讨论】:

  • "其他元素会收到点击:
    ..." 尝试先点击该元素。我猜有一个关闭按钮?例外是告诉您它将收到点击而不是您定位的元素。可能是因为它位于您尝试单击的元素的顶部。您还应该避免基于路径的选择器...(它们是“脆弱的”)使用 ID 或元素的独特内容来制作您的 XPATH。

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


【解决方案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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-08
    • 2017-04-16
    • 2015-06-13
    • 1970-01-01
    • 2021-06-07
    • 1970-01-01
    • 2018-03-14
    • 1970-01-01
    相关资源
    最近更新 更多