繁体   English   中英

Selenium Webdriver:Java:跨度Web元素标识

[英]Selenium Webdriver: Java: Span Web Element Identification

我正在使用Selenium WebDriver,正在尝试为span标签找到一个文本框,但是没有运气。 我能够找到一个类似div ID的文本框,所以我想它与Span ID有关。 使用xpath我正在生成此:

.//*[@id='rtbPrefix'] 

因此,这是我创建的WebElement:

@FindBy(xpath=".//*[@id='rtbPrefix']")
WebElement txtPrefix_AddContact;

请告诉我如何找到该项目,如果我应该使用CSS代替,如果可以,请告诉我如何找到该元素。 以下是以下元素的信息:

<table>
<tbody>
<tr>
<td class="ContactLeftLabelCol">
<td class="ContactLeftInputCol">
<span id="rtbPrefix_wrapper" class="riSingle RadInput RadInput_Metro" style="width: 50px;" title="Enter the contact's title (Mr., Ms., Dr., etc. ">
<input id="rtbPrefix" class="riTextBox riEnabled riHover" type="text" value="" title="Enter the contact's title (Mr., Ms., Dr., etc. " size="20" name="rtbPrefix"/>
<input id="rtbPrefix_ClientState" type="hidden" name="rtbPrefix_ClientState" autocomplete="off" value="{"enabled":true,"emptyMessage":"","validationText":"","valueAsString":"","lastSetTextBoxValue":""}"/>
</span>
</td>
</tr>

根据以下说明进行更新

    @FindBy(xpath = "//frame[.//input[@id = 'rtbPrefix']]")
        WebElement iframe_Contacts; 

public void Add_Prefix_NewContact(){

        iframe_Contacts.sendKeys("Mr.");
        //txtPrefix_AddContact.sendKeys("Mr.");
    }

根据回复中的每条指令更新了webelemnt,然后下面是在我的方法中实现的位置

public void CreateANewContact(){
    NewContactClick();

    base.getDriver().switchTo().frame(iframe_Contacts);
    Add_Prefix_NewContact();
    Add_FirstName_NewContact();
    Add_MiddleName_NewContact();
    Add_LastName_NewContact();
    Add_Suffix_NewContact();
    Add_Primary_NewContact();
    Add_JobTitle_NewContact();
    Add_Email_NewContact();
    Add_Phone_NewContact();
    Add_Mobile_NewContact();
    Add_Fax_NewContact();
    Add_Address_NewContact();
    Add_Country_NewContact();
    Add_Zip_NewContact();
    Add_City_NewContact();
    Add_State_NewContact();
    btnSaveClose_AddContact.click();
    Reporter.log("A New Contact Was Successfully Added!!!");
    base.getDriver().switchTo().defaultContent();
}

尝试:

WebDriver driver;
WebElement webElement = driver.findElement(By.id("rtbPrefix_wrapper"));

是的,它在iframe中

您必须切换到框架的上下文 ,然后在其中搜索元素:

driver.switchTo().frame("name_or_id");

WebElement webElement = driver.findElement(By.id("rtbPrefix"));

如果框架上没有定义idname ,则可以通过索引找到它:

driver.switchTo().frame(0);  // switch to the first frame

或者,使用findElement找到它:

WebElement frame = driver.findElement(By.xpath("//frame[.//input[@id = 'rtbPrefix']]");
driver.switchTo().frame(frame);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM