如何移动到删除按钮并单击Selenium中的按钮?

时间:2018-09-04 13:34:47

标签: selenium selenium-webdriver xpath salesforce

enter image description here

enter image description here

这是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>

请找到所附的屏幕截图:

enter image description here

2 个答案:

答案 0 :(得分:0)

上图所示的对话框不是警报,而只是HTML对话框。警报是Javascript alert之类,并且不包含HTML。

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

SELECT id, name, user_role
CASE
    WHEN user_role = 0 THEN "Member"
    WHEN user_role = 1 THEN "Admin"
    ELSE "Invalid!" 
END AS user_role_string
FROM user_table;

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

button[title='delete']

答案 1 :(得分:0)

       WebElement delete = (new WebDriverWait(driver, 30)).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[.='Delete']"))); 
       ((JavascriptExecutor) driver).executeScript("arguments[0].click();", delete);

非常感谢@theGuy你们真的很棒。 因此此脚本有效。 再次感谢