硒-如何单击文本框的空白区域

时间:2018-11-27 11:43:45

标签: java selenium selenium-webdriver xpath

我试图单击文本框的空白区域,而不是元素的文本。硒中有可能还是有其他棘手的方法?

enter image description here

红色箭头位于我要单击的位置

HTML:

<td class="ms-srm-Field-Data-Print" data-height="24" id="stan_servicecategoryid_d">
    <div id="stan_servicecategoryid" data-attributename="stan_servicecategoryid" data-formid="bfef905d-acd0-4ee9-a900-2034792789c1" data-fdeid="PrimaryEntity" data-layout="0" tabindex="1070" isinline="true" lookupstyle="single" lookupdialogmultipleselect="0" isinlinenewenabled="1" showasbreadcrumbcontrol="0" showglobalquickcreate="0" openfullform="1" addlookupimagebutton="" role="application" entitylogicalname="stan_servicecategory" entitydisplayname="Service Category" entitytypecode="10041" allowfilteroff="0" disablequickfind="0" disableviewpicker="1" disablemru="1" isdeduplookup="0" availableviewids="{EA878523-32E1-49BB-B0C5-FF97230B5AF2}" resolveemailaddress="0" allowunresolvedpartiesonemailsend="0" defaultviewid="{152B96D2-3BF4-469E-8BB4-52C639E51E50}" class="ms-srm-Inline-Chrome lookup nvarchar" aria-label="Service Category Unique identifier for Service Category associated with Service Request.  test Select the Enter key to edit the lookup field, and the Control and Enter keys to open the link">
        <div class="ms-srm-Inline-Value ms-srm-Inline-Lookup" style="display: block;">
            <label data-for-id="stan_servicecategoryid_label" id="Service Category_label">
                <div class="ms-srm-div-NotVisible">Service Category</div>
            </label>
            <span>
                <span contenteditable="false" class="ms-srm-Lookup-Item" id="stan_servicecategoryid_lookupValue" title="test" tabindex="0">test</span>
                <div class="ms-srm-Inline-EditIcon keyboardFocusClass" id="stan_servicecategoryid_lookupSearchIconDiv" style="display: none;">
                    <img src="/_imgs/imagestrips/transparent_spacer.gif" id="stan_servicecategoryid_lookupSearchIcon" class="ms-srm-ImageStrip-search_normal ms-srm-InlineLookupEdit ms-srm-EditLookup-Image" alt="">
                </div>
            </span>
        </div>
        <div class="ms-srm-Inline-Edit ms-srm-Inline-Lookup" style="display: none;" lookuptypes="10041">
            <table id="stan_servicecategoryid_lookupTable" cellpadding="0" cellspacing="0" lookupid="stan_servicecategoryid" class="ms-srm-Lookup" aria-labelledby="stan_servicecategoryid_c stan_servicecategoryid_w" controlmode="normal" style="width: 100%;table-layout: fixed;">
                <tbody>
                    <tr>
                        <td valign="top">
                            <div id="stan_servicecategoryid_lookupDiv" ime-mode="auto" class="ms-srm-Lookup ms-srm-InlineLookupEdit" tabindex="0" aria-labelledby="stan_servicecategoryid_lookupedit" title="Service Category Unique identifier for Service Category associated with Service Request.  test" style="display: block; background-color: rgb(171, 192, 231); width: 100%;">
                                <ul class="ms-srm-InlineLookupEdit">
                                    <li style="display: inline; white-space: nowrap;">
                                        <span class="ms-srm-Lookup-Item" contenteditable="false" isinlinelookup="true">
                                            <span class="ms-srm-LookupItem-Name" id="stan_servicecategoryid_lookupEditSpan" title="test" otype="10041">test</span>
                                        </span>
                                    </li>
                                </ul>
                            </div>
                            <label id="stan_servicecategoryid_lookupedit" class="ms-srm-Hidden-NoBehavior" for="stan_servicecategoryid_ledit">Enter a search term, then select the Enter key to search</label>
                            <input class="ms-srm-InlineInput ms-srm-InlineLookupEdit" ime-mode="auto" aria-labelledby="stan_servicecategoryid_lookupedit stan_servicecategoryid_w" type="text" id="stan_servicecategoryid_ledit" maxlength="1000" style="display: none;">
                        </td>
                        <td class="ms-srm-InlineLookupEdit-Box"></td>
                        <td class="Lookup_RenderButton_td" id="stan_servicecategoryid_lookupSearch" style="vertical-align:top;width:22px;">
                            <img src="/_imgs/search_normal.gif" id="stan_servicecategoryid_i" alt="Select a value." class="ms-srm-InlineLookupEdit" title="Select a value.">
                            <a href="#" id="stan_servicecategoryid_a" tabindex="-1" title="Select a value."></a>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
        <span class="ms-srm-Inline-LockIcon" id="stan_servicecategoryid_lock" style="display: none;">
            <img src="/_imgs/imagestrips/transparent_spacer.gif" class="ms-srm-ImageStrip-inlineedit_locked" alt="Locked control">
        </span>
        <span class="ms-srm-Inline-fieldChangeIndicatorIcon" id="stan_servicecategoryid_fieldChangeIndicatorImage" style="visibility: hidden;">
            <img src="/_imgs/imagestrips/transparent_spacer.gif" class="ms-srm-ImageStrip-field_change_indicator" alt="">
        </span>
        <span id="stan_servicecategoryid_warnSpan" class="ms-srm-Inline-WarningIcon" title="" style="display: none;" role="alert" aria-live="assertive" aria-atomic="true">
            <img src="/_imgs/imagestrips/transparent_spacer.gif" class="ms-srm-ImageStrip-inlineedit_warning" alt="Error" id="stan_servicecategoryid_warn">
            <div id="stan_servicecategoryid_w" class="ms-srm-Hidden-NoBehavior"></div>
        </span>
    </div>
    <div style="display:none;"></div>
</td>

硒:

driver.FindElement(By.Id("stan_servicecategoryid_lookupValue").Click();

new Actions(driver).MoveToElement(driver.FindElement(by)).SendKeys(Keys.Clear).Perform(); // This will clear the value of the text box

1 个答案:

答案 0 :(得分:0)

使用ACTIONS(或Python中的ActionChains)

如果模拟moveToElement(element)并单击(),它将完全单击元素的中心。如果文本很长,并且上面的文本仍然在文本上单击,请使用moveToElement(带有偏移),并根据需要为其提供x偏移。

Java:

Actions action = new Actions(driver);
action.moveToElement(element).click().perform();

action.moveToElement(element, x-offset, 0).click().perform();

Python:

from selenium.webdriver.common.action_chains import ActionChains as AC

AC(driver).move_to_element(element).click().perform()

AC(driver).move_to_element_with_offset(element, x-offset, 0).click().perform()