简体   繁体   中英

Element not found on the page by selenium Webdriver

I need to check if a particular text is present in a span that has a composite class but selenium does not find this element on the page at all.

I have already tried with class and xpath, but it is not found.

Full html code where I need to get the text:

<span class="mn-person-info__occupation mn-person-info__occupation--card-layout Sans-13px-black-55%">
      Consultor de RH - HProjekt
</span>

Full html code:

<div class="mn-person-info__card-details mt1">
<a data-control-name="pymk_profile" href="/in/kaique-soares-04798354/" id="ember1053" class="mn-person-info__link ember-view">    <span class="visually-hidden">Nome do usuário</span>
    <span class="mn-person-info__name
        mn-person-info__name--card-layout Sans-15px-black-85%-semibold">
      Kaique Soares
    </span>
    <span class="visually-hidden">Cargo do usuário</span>
    <span class="mn-person-info__occupation
        mn-person-info__occupation--card-layout Sans-13px-black-55%">
      Consultor de RH - HProjekt
    </span>
</a>
    <div class="mn-person-info__shared-insights display-flex justify-center pt1 ph2">
    <button data-control-name="see_all_common_connections" class="mn-person-info__shared-insights-btn Sans-13px-black-55%" data-ember-action="" data-ember-action-1256="1256">
      <span class="mn-person-info__shared-insights-icon svg-icon-wrap"><li-icon aria-hidden="true" type="in-common-icon" size="small"><svg viewBox="0 0 24 24" width="24px" height="24px" x="0" y="0" preserveAspectRatio="xMinYMin meet" class="artdeco-icon"><g class="small-icon" style="fill-opacity: 1">
        <path d="M11,3C9.9,3,8.9,3.4,8,4C5.8,2.3,2.7,2.8,1,5s-1.2,5.3,1,7c0.9,0.6,1.9,1,3,1s2.1-0.4,3-1c2.2,1.7,5.3,1.2,7-1s1.2-5.3-1-7C13.1,3.4,12.1,3,11,3z M1.9,8c0-1.7,1.4-3.1,3.1-3.1c0.6,0,1.2,0.2,1.7,0.5c-1,1.6-1,3.6,0,5.2c-1.4,1-3.4,0.6-4.3-0.8C2.1,9.2,1.9,8.6,1.9,8z M11,11.1c-0.6,0-1.2-0.2-1.7-0.5c1-1.6,1-3.6,0-5.2c1.4-1,3.4-0.6,4.3,0.9s0.6,3.4-0.9,4.3C12.2,10.9,11.6,11.1,11,11.1z"/>
      </g></svg></li-icon></span>
      <span class="mn-person-info__shared-insights-count">
          Fernando Abrantes e mais 76 pessoas
      </span>
    </button>
</div>

</div>

I tried these ways and even put waits and still not find the element:

String text = driver.findElement(By.className("mn-person-info__occupation.mn-person-info__occupation--card-layout.Sans-13px-black-55%")).getText();
System.out.println(text);

driver.findElement(By.className("mn-person-info__occupation.mn-person-info__occupation--card-layout.Sans-13px-black-55%")).getText();

WebElement text = driver.findElement(By.className("mn-person-info__occupation.mn-person-info__occupation--card-layout.Sans-13px-black-55%")).getText();
System.out.println("text");

The method By.className takes classes separated by a space but you are using a dot.

One class should be enough in your case :

By.className("mn-person-info__occupation--card-layout")

, or with a CSS selector:

By.cssSelector(".mn-person-info__occupation--card-layout")

Note that some of the classes have an illegal character like % . For those you'll have to use a CSS selector with a filter on the attribute:

By.cssSelector(".mn-person-info__occupation--card-layout[class*='Sans-13px-black-55%']")

Can you please try with this xpath.

String content =  driver.findElement(by.xpath("//div[class='mn-person-info__card-details mt1']/a[1]/spam[3]")).getText();

Hope this helps. Thanks.

Try this:

WebElement myelement = driver.findElement(By.xpath("//div[@class='mn-person-info__card-details mt1']a[1]/span[3]"));

//Now get the text.
String mytext = descriptionEle.getText();

如果您尝试如何

String content =  driver.findElement(by.xpath("//span[class='mn-person-info__occupation']")).getText();

The issue is caused by the By selector. It looks wonkie and selenium can't handle it. Selenium doesn't like having % in the selectors.

If you want to use by classname then i would suggest going an easier route.

  String text = driver.findElement(By.className("mn-person-info__occupation")).getText();
  System.out.println(text);

Or another option would by using.

String text2 = driver.findElement(By.className("mn-person-info__occupation--card-layout")).getText();
System.out.println(text2);

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