简体   繁体   中英

Unable to find element with xpath == “//*[text()[contains(.,' ”+ word + “ ')]]”

I had used sapui5 with xml view and java project, using Selenium 1.82 for automated testing.

xml code like this

<core:FragmentDefinition xmlns:core="sap.ui.core" xmlns:mvc="sap.ui.core.mvc" xmlns="sap.m" xmlns:f="sap.ui.layout.form" xmlns:l="sap.ui.layout">
  <Panel expanded="true" expandable="true" width="auto">
    <headerToolbar>
      <Toolbar>
        <Title text="{i18n>POLICY_HOLDER_INFO_BASIC_INFO}" />
      </Toolbar>
    </headerToolbar>
    <content>
      <f:SimpleForm layout="ResponsiveGridLayout" labelSpanL="3" labelSpanM="3" labelSpanS="12" editable="true">
        <f:content>
            <VBox>
                <CheckBox id="SameWithInsured" text="{i18n>POLICY_HOLDER_INFO_SAME_WITH_INSURED}" groupName="holderstatus1" selected="{searchModel>/d/SamewithInsured}" select="changeSameWithInsured"/>
                <layoutData>
                    <l:GridData span="L12 M12 S12" indent="L3 M3 S3"></l:GridData>
                </layoutData>
            </VBox>
          <Label text="{i18n>POLICY_HOLDER_INFO_IDENTIFICATION}" />
          <VBox>
            <RadioButton id="PHPerson" text="{i18n>POLICY_HOLDER_INFO_INDIVIDUAL}" enabled="{ path: '/d/SamewithInsured', model: 'searchModel', formatter: '.isPHDEditEnabled'}" groupName="holderstatus" selected="{= ${searchModel>/d/Person}===true}" select="clearRelation"/>
          </VBox>
          <VBox>
            <RadioButton id="PHLegalEntity" text="{i18n>POLICY_HOLDER_INFO_CORPORATION}" enabled="{ path: '/d/SamewithInsured', model: 'searchModel', formatter: '.isLegalEntityEnabled'}" groupName="holderstatus" selected="{=${searchModel>/d/LegalEntity}===true}" select="clearRelation"/>
          </VBox>
          <Label text="{i18n>POLICY_HOLDER_INFO_RELATIONSHIP}"/>
          <Select id="relationSelect" width="100%" selectedKey="{searchModel>/d/RelationshipKey}" enabled="{ path: '/d/SamewithInsured', model: 'searchModel', formatter: '.isPHDEditEnabled'}" change="relationSelectChange">
            <items>
              <core:Item key="1" text="{i18n>POLICYHOLDER_BASICINFO_RELATION_BLANK}" />
              <core:Item key="2" text="{i18n>POLICYHOLDER_BASICINFO_RELATION_ELF}" enabled="{=${searchModel>/d/LegalEntity}===false}"/>
              <core:Item key="3" text="{i18n>POLICYHOLDER_BASICINFO_RELATION_SPOUSE}" enabled="{=${searchModel>/d/LegalEntity}===false}"/>
            </items>
          </Select>
        </f:content>
      </f:SimpleForm>
     </content>
  </Panel>
</core:FragmentDefinition>

My test script is(driver have been instanced)

WebDriverWait wait = new WebDriverWait(driver,30);
WebElement e = wait.until(ExpectedConditions.elementToBeClickable(dirver.findElement(By.xpath("//*[text()[contains(.,'" + word + "')]]"))));
e.click;

I also tried

WebElement e = wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.xpath("//core:FragmentDefinition/Panel/content/f:SimpleForm/f:content/Select/items/core:Item[contains(.,'" + word +"')]"))));

got org.openqa.selenium.InvalidSelectorException: Unable to locate an element with the xpath expression //core:FragmentDefinition/Panel/content/f:SimpleForm/f:content/Select/items/core:Item[contains(.,'配偶')] because of the following error: Error: Namespace prefix not declared: core For documentation on this error, please visit: http://seleniumhq.org/exceptions/invalid_selector_exception.html

您可以为xpath尝试以下语法吗?

"//*[contains(text(), '"+word+"')]"

我已经通过使用隐式等待而不是显式等待来解决此问题。

driver.manage().timeouts().implicitlyWait(30 , TimeUnit.SECONDS);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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