如何使用Java获取具有特定id或CSS的元素的文本。

huangapple go评论97阅读模式
英文:

How Can I get find text of element of id , css with Java

问题

我正在尝试使用Java编写一个测试场景(使用Selenium Webdriver、Junit和TestNG)。

@Test
public void clickAllSee() {
    page.myProfilPages().clickSeeAllWriting();  // 这里是我想要去的地方。
}

在这个如何使用Java获取具有特定id或CSS的元素的文本。
这也是我的HTML代码:

<div class="row title-body no-gutters">
    <div class="col"><div class="title"><p>Özgeçmişler</p></div></div>
    <div class="col"><div id="new-resume" class="new-resume-btn">
        <a target="_blank" class="btn">
            <i class="material-icons">add</i>
            <span id="create-resume" class="d-sm-none d-block">Yeni</span>
            <span id="mobil-create-resume" class="d-sm-block d-none">Yeni Özgeçmiş Oluştur</span>
        </a>
    </div></div>
</div>

我想要获取"Yeni Özgeçmiş Oluştur Button"的文本,即"Yeni Özgeçmiş Oluştur"。当我尝试使用下面代码中的"cssSelector"或"id"时,它们无法工作。

String h = TLDriver.getDriver().findElement(By.cssSelector(".row.load-more-button")).getText(); // 这里的代码会报错
String h = TLDriver.getDriver().findElement(By.id("mobil-create-resume")).getText();
Assert.assertEquals(h, "Yeni Özgeçmiş Oluştur");

- 我如何使用findElement(By. ...)来获取"Yeni Özgeçmiş Oluştur"的文本?

在场景失败后,控制台会显示类似以下错误:

这是关于"csselement"的错误:org.openqa.selenium.NoSuchElementException: no such element:无法定位元素:{"method":"css selector","selector":".row.load-more-button"}

英文:

I am trying to write a Test Scenario with Java ( using Selenium Webdriver, Junit ,testng).**

@Test

public void clickAllSee(){
    page.myProfilPages().clickSeeAllWriting();  //here is going to where I want to go.

In this 如何使用Java获取具有特定id或CSS的元素的文本。
Also this is my html

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;div class=&quot;row title-body no-gutters&quot;&gt;&lt;div class=&quot;col&quot;&gt;&lt;div class=&quot;title&quot;&gt;&lt;p&gt;&#214;zge&#231;mişler&lt;/p&gt;&lt;/div&gt;&lt;/div&gt; &lt;div class=&quot;col&quot;&gt;&lt;div id=&quot;new-resume&quot; class=&quot;new-resume-btn&quot;&gt;&lt;a target=&quot;_blank&quot; class=&quot;btn&quot;&gt;&lt;i class=&quot;material-icons&quot;&gt;add&lt;/i&gt; &lt;span id=&quot;create-resume&quot; class=&quot;d-sm-none d-block&quot;&gt;Yeni&lt;/span&gt; &lt;span id=&quot;mobil-create-resume&quot; class=&quot;d-sm-block d-none&quot;&gt;Yeni &#214;zge&#231;miş Oluştur&lt;/span&gt;&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

<!-- end snippet -->

I want to get text of "Yeni Özgeçmiş oluştur Button" which is "Yeni Özgeçmiş Oluştur" . When I am trying to use "cssSelector" or "id" like down my codes, its not working.

    String h = TLDriver.getDriver().findElement(By.cssSelector(&quot;.row.load-more-button&quot;)).getText(); //here codes give error
   String h = TLDriver.getDriver().findElement(By.id(&quot;mobil-create-resume&quot;)).getText();


     Assert.assertEquals(h,&quot;Yeni &#214;zge&#231;miş Oluştur&quot;);   

}

- How Can I reach "Yeni Özgeçmiş Oluştur" text using findElement(By. ...) ?

After Fails scenario , console gives like this error;

This is error of "csselement" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {&quot;method&quot;:&quot;css selector&quot;,&quot;selector&quot;:&quot;.row.load-more-button&quot;}

答案1

得分: 0

Yeni Özgeçmiş Oluştur Butonu:

(By.XPATH, "//*[text()='Yeni Özgeçmiş Oluştur']")

Özgeçmiş Adı Girişi:

(By.XPATH, "//input[@id='text']")

Özgeçmiş Oluştur Butonu:

(By.XPATH, "//button[@class='btn btn-secondary']")
英文:

Yeni Özgeçmiş Oluştur Button:

(By.XPATH, &quot;//*[text()=&#39;Yeni &#214;zge&#231;miş Oluştur&#39;]&quot;)

Özgeçmiş Name Input:

(By.XPATH, &quot;//input[@id=&#39;text&#39;]&quot;)

Özgeçmiş Oluştur Button:

(By.XPATH, &quot;//button[@class=&#39;btn btn-secondary&#39;]&quot;)

huangapple
  • 本文由 发表于 2020年5月3日 19:48:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/61573923.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定