【问题标题】:How to move to the delete button and click on that in Selenium?如何移动到删除按钮并在 Selenium 中单击该按钮?
【发布时间】:2019-02-09 13:32:33
【问题描述】:

这是一个 Salesforce 亮化页面,在删除对象时,我得到一个模式对话框,我无法移动以单击它。警报类无法移动到页面上。

请在下面找到代码:

 public static void Delete_Object(WebDriver driver) throws InterruptedException

 {
      // (new WebDriverWait(driver, 30)).until(ExpectedConditions.elementToBeClickable(By.xpath(("//label[contains(@for,'globalQuickfind')]/preceding::div/input[contains(@id,'globalQuickfind')]")))).sendKeys("java dev");

     try
     {

       (new WebDriverWait(driver, 30)).until(ExpectedConditions.elementToBeClickable(By.xpath(("//span[.='Java_dev__c']//following::td[5]//a")))).click();


       (new WebDriverWait(driver, 30)).until(ExpectedConditions.elementToBeClickable(By.xpath(("//span[.='Delete']//span")))).click();

       Thread.sleep(5000);

      (new WebDriverWait(driver, 30)).until(ExpectedConditions.elementToBeClickable(By.xpath(("//button[@title=\"Close this window\"]")))).click();

     }

   catch(Exception e)
     {
       System.out.println(e.getMessage());
     }


 }

这里是html代码:

<div class="panel slds-modal slds-fade-in-open" aria-labelledby="title_4828:0" tabindex="-1" role="dialog" aria-modal="true" data-aura-rendered-by="4842:0">
  <div class="modal-container slds-modal__container" data-aura-rendered-by="4843:0">
<div class="modal-header slds-modal__header empty slds-modal__header--empty" data-aura-rendered-by="4844:0">
  <!--render facet: 4845:0--><!--render facet: 4846:0--><!--render facet: 4847:0-->
  <button class="slds-button slds-modal__close closeIcon slds-button_icon-bare slds-button_icon-inverse" type="button" title="Close this window" data-aura-rendered-by="4832:0">
    <lightning-primitive-icon data-aura-rendered-by="4833:0" lightning-primitive-icon_primitive-icon="">
      <svg focusable="false" data-key="close" aria-hidden="true" class="slds-button__icon slds-button__icon_large slds-button_icon-inverse" lightning-primitive-icon_primitive-icon="">
        <use xlink:href="/_slds/icons/utility-sprite/svg/symbols.svg?cache=9.8.0#close" lightning-primitive-icon_primitive-icon=""></use>
      </svg>
    </lightning-primitive-icon>
    <span class="slds-assistive-text" data-aura-rendered-by="4835:0">Close this window</span>
  </button>
</div>
<div class="modal-body scrollable slds-modal__content slds-p-around--medium" data-aura-rendered-by="4850:0" data-scoped-scroll="true">
  <!--render facet: 4837:0-->
  <div data-aura-rendered-by="4798:0" class="objectManagerCustomObjectDeleteWarning" data-aura-class="objectManagerCustomObjectDeleteWarning">
    <p data-aura-rendered-by="4799:0">Deleting a custom object does the following:</p>
    <ul data-aura-rendered-by="4801:0">
      <li data-aura-rendered-by="4802:0">Deletes all the data that currently exists in all records of that custom object</li>
      <li data-aura-rendered-by="4804:0">Deletes all tracked history data associated with the custom object</li>
      <li data-aura-rendered-by="4806:0">Deletes the custom tab and list views for the object</li>
      <li data-aura-rendered-by="4808:0">Deletes workflow rules and actions that use the object</li>
      <li data-aura-rendered-by="4810:0">Hides the custom object definition and all related definitions</li>
      <li data-aura-rendered-by="4812:0">Hides the object's data records and all related data records</li>
      <li data-aura-rendered-by="4814:0">Disables report types for which this is the main object</li>
      <li data-aura-rendered-by="4816:0">Disables custom reports for which this is the main object</li>
      <li data-aura-rendered-by="4818:0">Deactivates custom formula fields on the object</li>
      <li data-aura-rendered-by="4820:0">Deactivates custom validation rules and approval processes on the object</li>
      <li data-aura-rendered-by="4822:0">Deactivates and deletes the Lightning Pages associated with the custom object.</li>
    </ul>
    <p data-aura-rendered-by="4824:0">In order to preserve a record of data for future reference, run a data export before deleting custom objects by clicking Data | Data Export.</p>
    <p data-aura-rendered-by="4826:0">After you've deleted this custom object, it appears in the deleted objects list for 15 days. During that time, you can either undelete it to restore it and all the data stored in it, or delete it permanently. When you undelete a custom object, some of its properties might be lost or changed.  After 15 days, the object and its data are permanently deleted.</p>
  </div>
</div>
<div class="modal-footer slds-modal__footer" data-aura-rendered-by="4871:0">
  <div data-aura-rendered-by="4855:0" class="forceModalActionContainer--footerAction forceModalActionContainer" data-aura-class="forceModalActionContainer--footerAction forceModalActionContainer">
    <button class="slds-button slds-button--neutral uiButton--default uiButton--brand uiButton forceActionButton" aria-live="off" type="button" title="delete" data-aura-rendered-by="4862:0" data-aura-class="uiButton--default uiButton--brand uiButton forceActionButton">
      <!--render facet: 4863:0--><span class=" label bBody" dir="ltr" data-aura-rendered-by="4865:0">Delete</span><!--render facet: 4860:0-->
    </button>
  </div>
</div>
  </div>
</div>

请查看附件截图:

【问题讨论】:

  • 以文本格式粘贴 HTML。
  • 当你有文本输出时,don't take a picture but copy paste the output in your POST 也可以右键复制html -> 复制为outerHTML。
  • 你可以在截图中看到@cruisepandey
  • 不,需要div的innerHTML
  • @AnuragShrivastava :除非必要,否则发布图片绝不是一件好事。以文本格式共享 HTML 代码。以便我们更有效地帮助您。

标签: selenium selenium-webdriver xpath salesforce


【解决方案1】:

图中的对话框不是警报,它只是一个 HTML 对话框。警报是 Javascript alert 等,不包含 HTML。

要单击对话框中的删除按钮,您只需要一个 CSS 定位器,如

button[title='delete']

我已经清理了你的代码。由于您的所有等待都是相同的,因此您可以定义一个等待并重用它。

public static void Delete_Object(WebDriver driver) throws InterruptedException
{
    try
    {
        WebDriverWait wait = new WebDriverWait(driver, 30);
        wait.until(ExpectedConditions.elementToBeClickable(By.xpath(("//span[.='Java_dev__c']//following::td[5]//a")))).click();
        wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("button[title='delete']"))).click();
        wait.until(ExpectedConditions.elementToBeClickable(By.xpath(("//button[@title=\"Close this window\"]")))).click();
    }
    catch(Exception e)
    {
        System.out.println(e.getMessage());
    }
}

【讨论】:

  • 只是出于好奇,为什么它必须是一个 css 选择器?我确实更喜欢 css 定位器,因为它速度很快,但在这种情况下 xpath 会不起作用吗?
  • @theGuy 它不一定是 CSS 选择器我只是更喜欢它们,因为它们更快、更好的支持等等。我想不出任何 CSS 选择器可以找到的定位器可以'不被转换成 XPath...但是有一些 XPaths 不能被转换成 CSS 选择器。例如,基于包含的文本定位元素或进行 DOM 导航的任何事情都不能在 CSS 中完成。
  • @theGuy 但您无法为我提供问题的答案。
  • @theGuy 如果你能在这种情况下帮助我,那就太好了
  • 我猜你在我不在的时候离开了房间,我在我最后一次聊天评论中要求你尝试 JavaScript 解决方案。另外,您可以尝试 JeffC 提供的答案,
【解决方案2】:
       WebElement delete = (new WebDriverWait(driver, 30)).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[.='Delete']"))); 
       ((JavascriptExecutor) driver).executeScript("arguments[0].click();", delete);

非常感谢@theGuy 你们真的很棒。 所以这个脚本已经奏效了。 再次感谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-19
    • 2013-12-24
    • 2021-09-05
    相关资源
    最近更新 更多