【问题标题】:Cant select elements on Webpage无法选择网页上的元素
【发布时间】:2018-07-04 00:28:48
【问题描述】:

我在尝试选择下拉菜单中的两个元素时遇到了重大问题。我尝试了 xpath、链接文本和 css 选择器,但它不会选择密码按钮或注销按钮。

用于密码按钮的 Xpath:"//*[@id='app']/header/div[3]/nav/ul/li/a"

用于注销按钮的 CSS:["data-logged-in-log-out-button"] 用于注销按钮的 XPath:"//*[@id='app']/header/div[3]/nav/ul/a"

我得到的选择密码的错误是:

org.openqa.selenium.WebDriverException:未知错误:元素 ... 在点 (989, 233) 处不可点击。 其他元素会收到点击:...

【问题讨论】:

  • 请添加您使用的确切 xpath 或其他定位器。
  • 您要选择什么 - 注销?你得到的例外是什么?您能分享您尝试过的代码吗?
  • 你能以文本格式分享这个 HTML 代码吗?
  • 我已经添加了我尝试过的 Xpath 和选择器,我试图选择密码按钮和注销按钮。选择密码时出现的错误是:org.openqa.selenium.WebDriverException:未知错误:元素 ... 在点 (989, 233) 处不可点击。其他元素会收到点击:
    ...
  • 我明白了。当页面加载时,您是否有一些覆盖(一些半透明的屏幕)?看起来您的定位器被其他元素覆盖了。

标签: java selenium ui-automation


【解决方案1】:

请尝试使用以下 XPath 以及显式等待条件。

XPath:

//*[contains(text(),'Log out')]

【讨论】:

    【解决方案2】:

    你能不能试试-

    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    public class A {
    
        public static void main(String[] args) {
            WebDriver driver = new ChromeDriver();
            driver.get("url here");
            WebDriverWait wait = new WebDriverWait(driver, 10);
            wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Log Out")));
            driver.findElement(By.linkText("Log Out")).click();;
        }
    
    }
    

    如果还是不行,请尝试-

    import org.openqa.selenium.By;
    import org.openqa.selenium.JavascriptExecutor;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    
    public class A {
    
        public static void main(String[] args) {
            WebDriver driver = new ChromeDriver();
            driver.get("url here");
    
            JavascriptExecutor js = (JavascriptExecutor) driver;
            js.executeScript("arguments[0].click();", driver.findElement(By.linkText("Log Out")));
        }
    
    }
    

    【讨论】:

    • 不,它没有捡起它,java.lang.NullPointerException,它似乎根本找不到元素
    • 然后,伙计,您需要找出覆盖该元素的内容。从给定的html代码中,还不清楚。看起来<div class="borderStyles_5fsn69-o_O-accountSettingsUserInfo_l6g6b1">...</div> 正在覆盖它。你能试着检查一下,看看有没有什么东西吗?
    猜你喜欢
    • 2010-12-08
    • 2018-07-13
    • 2021-03-18
    • 2017-04-30
    • 2017-10-14
    • 1970-01-01
    • 2017-06-26
    • 2018-08-05
    • 2017-01-03
    相关资源
    最近更新 更多