【问题标题】:Unable to click on Save button for naukri.com无法单击 naukri.com 的保存按钮
【发布时间】:2018-02-03 18:34:15
【问题描述】:

下面的代码是点击保存按钮,但它不起作用,甚至错误也不显示,我也共享了 DOM,请帮助我尝试了类名,xpath,csslocator,Javascript 向下滚动,Actions 类,但是它仍然不适合我,请帮助。

WebElement element = driver.findElement(By.xpath(".//*[@type='submit']"));
Actions action = new Actions(driver);
action.moveToElement(element).click().build().perform()

  <div class="formRow">
  <div class="formRow">
  <div class="blueBut1 ml124">
  <button class="w150bt fl" type="submit" value="Save Changes">
  <a class="fl mt10 ml8" href="/Profile/view?id=&altresid=" rel="last">
  </div>
  </div>
  </form>
  </div>
  </div>
  </div>

【问题讨论】:

  • 查看driver.findElements(By.xpath(".//*[@type='submit']")).length 的输出并检查多个元素是否符合您的描述
  • 试试 xpath //button[@value='Save Changes' and @type='submit'] or //button[@value='Save Changes']

标签: java selenium automation webdriver


【解决方案1】:

尝试等待元素变为可点击。

WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//*[@type='submit']"))).click();

【讨论】:

    【解决方案2】:

    等待这个 xpaths 试试

    //button[@value='Save Changes']
    //button[@value='Save Changes' and @type='submit']
    

    使用此代码

    WebElement submitButton= driver.findElement(By.xpath("//button[@value='Save Changes']"));
    WebDriverWait waitForElement = new WebDriverWait(driver, 30);
    waitForElement.until(ExpectedConditions.elementToBeClickable(submitButton))).click();
    

    WebElement submitButton= driver.findElement(By.xpath("//button[@value='Save Changes' and @type='submit']"));
    WebDriverWait waitForElement = new WebDriverWait(driver, 30);
    waitForElement.until(ExpectedConditions.elementToBeClickable(submitButton))).click();
    

    【讨论】:

      【解决方案3】:

      试试下面的代码:-

      WebElement element = driver.findElement(By.xpath("//button[@type='submit' and @value='Save Changes']"));
      JavascriptExecutor executor = (JavascriptExecutor)driver;
      executor.executeScript("arguments[0].click();", element);
      

      【讨论】:

      • 使用 JSE 不是一个好习惯。这不是用户可以执行的场景。更好的做法是找出实际问题并解决它,以便脚本以用户的方式与页面交互。
      猜你喜欢
      • 2022-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-07
      • 2018-05-24
      • 2013-11-08
      • 1970-01-01
      • 2018-04-10
      相关资源
      最近更新 更多