如何获取Selenium文本,但当产品有折扣时元素会发生变化。

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

How to get Selenium Text But When Product Has Discount Element Changes

问题

JAVA我从网站上随机选择产品有时有折扣有时没有

例如<br><br>
 如何获取 748无折扣产品<br><br>
        或者 如何获取 419有折扣产品<br><br>
当产品有折扣时元素为

    <div class="pb-basket-item-price">748 TL</div>

 
当其他产品没有折扣时元素为

    <div class="pb-basket-item-price">
    <span>499 TL</span>
    "419 TL"</div>
英文:

(JAVA) I select random product from the site. Sometimes it has discount sometimes it does not.

For example:<br><br>
How can I get 748(product without discount)<br><br>
or How can I get 419 (product with discount)<br><br>
When Product has discount The element is :

&lt;div class=&quot;pb-basket-item-price&quot;&gt;748 TL&lt;/div&gt;

When Other Product doesnt have discount The element is :

&lt;div class=&quot;pb-basket-item-price&quot;&gt;
&lt;span&gt;499 TL&lt;/span&gt;
&quot;419 TL&quot;&lt;/div&gt;

答案1

得分: 1

List<WebElement> elements = driver.findElements(By.xpath("//div[contains(@class, 'pb-basket-item-price')]"));
for (WebElement element : elements) {
    String str = element.getText();

    int cntTL = (str.length() - str.replace("TL", "").length()) / 2;
    if (2 == cntTL) {
        str = str.split("TL")[1].replace("\"", "").trim() + " TL";
    }

    System.out.println("this is what you need: " + str);
    // str is what you want!
}
英文:
List&lt;WebElement&gt; elements = driver.findElements(By.xpath(&quot;//div[contains(@class, &#39;pb-basket-item-price&#39;)]&quot;));
for (WebElement element : elements) {
    String str = element.getText();
    System.out.println(&quot;original string: &quot; + str);

    if (str.contains(&quot;\&quot;&quot;)) {
        str = str.split(&quot;\&quot;&quot;)[1];
    }
    System.out.println(&quot;this is what you need: &quot; + str);
}

, below is the running log:

original string: 748 TL
this is what you need: 748 TL

original string: 499 TL &quot;419 TL&quot;
this is what you want: 419 TL

EDIT

Modify according to the question owner's comments.

Suppose: HTML looks like:

&lt;div class=&quot;pb-basket-item-price&quot;&gt;748 TL&lt;/div&gt;

&lt;div class=&quot;pb-basket-item-price&quot;&gt;
&lt;span&gt;499 TL&lt;/span&gt;
&quot;419 TL&quot;&lt;/div&gt;

&lt;div class=&quot;pb-basket-item-price&quot;&gt;
&lt;span&gt;3.374,34 TL&lt;/span&gt;
2.339 TL&lt;/div&gt;

code:

List&lt;WebElement&gt; elements = driver.findElements(By.xpath(&quot;//div[contains(@class, &#39;pb-basket-item-price&#39;)]&quot;));
for (WebElement element : elements) {
    String str = element.getText();

	int cntTL = (str.length() - str.replace(&quot;TL&quot;, &quot;&quot;).length()) / 2;
    if (2 == cntTL) {
        str = str.split(&quot;TL&quot;)[1].replace(&quot;\&quot;&quot;, &quot;&quot;) + &quot; TL&quot;;
    }

    System.out.println(&quot;this is what you need: &quot; + str);
    // str is what you want!
}

答案2

得分: 0

首先,您需要首先获取元素,如下所示:

List<WebElement> elements = driver.findElements(By.xpath("//div[contains(@class, 'pb-basket-item-price')]"));

然后,您可以遍历 WebElement 列表并检查元素应该具有的特定文本。

英文:

You need to get the elements first of all like:

List&lt;WebElement&gt; elements = driver.findElements(By.xpath(&quot;//div[contains(@class, &#39;pb-basket-item-price&#39;)]&quot;));

Then you may iterate through the List of Webelements and check for the specific text the element should have.

答案3

得分: 0

抓取产品标签,然后向内部进行检查,判断是否有标签。

WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement outertag = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(@class, 'pb-basket-item-price')]")));
List<WebElement> innertag = outertag.findElements(By.xpath("//span"));
if (innertag.size() > 0) {
    System.out.println(innertag.get(0).getText());
} else {
    System.out.println(outertag.getText());
}
英文:

Grab the product tag and then proceed inwards checking if it has a span or not.

WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement outertag = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(&quot;//div[contains(@class, &#39;pb-basket-item-price&#39;)&quot;)));
List&lt;WebElement&gt; innertag = outertag.findElements(By.xpath(&quot;//span&quot;));
if(innertag.size()&gt;0){
System.out.println(innertag.get(0).getText());
}
else{
System.out.println(outertag.getText());
}

答案4

得分: 0

以下是翻译后的内容:

要打印文本,您可以使用以下任一定位策略

  • 748

    • 使用 css_selectorget_attribute()

      print(driver.find_element_by_css_selector("div.pb-basket-item-price").get_attribute("innerHTML"))
      
    • 使用 xpathtext 属性:

      print(driver.find_element_by_xpath("//div[@class='pb-basket-item-price']").text)
      
  • 419

    • 使用 css_selectortextContent

      print(driver.execute_script('return arguments[0].lastChild.textContent;', driver.find_element_by_css_selector("div.pb-basket-item-price")).strip())
      
    • 使用 xpathtextContent

      print(driver.execute_script('return arguments[0].lastChild.textContent;', driver.find_element_by_xpath("//div[@class='pb-basket-item-price']").strip())
      
英文:

To print the texts you can use either of the following Locator Strategies:

  • 748

    • Using css_selector and get_attribute():

      print(driver.find_element_by_css_selector(&quot;div.pb-basket-item-price&quot;).get_attribute(&quot;innerHTML&quot;))
      
    • Using xpath and text attribute:

      print(driver.find_element_by_xpath(&quot;//div[@class=&#39;pb-basket-item-price&#39;]&quot;).text)
      
  • 419

    • Using css_selector and textContent:

      print(driver.execute_script(&#39;return arguments[0].lastChild.textContent;&#39;, driver.find_element_by_css_selector(&quot;div.pb-basket-item-price&quot;)).strip())
      
    • Using xpath and textContent:

      print(driver.execute_script(&#39;return arguments[0].lastChild.textContent;&#39;, driver.find_element_by_xpath(&quot;//div[@class=&#39;pb-basket-item-price&#39;]).strip())
      

答案5

得分: 0

所以背后的思想是,

首先,您需要找出这些元素并将它们转换为列表。

$x("//div[contains(@class, 'pb-basket-item-price')]")

一旦您将列表放置好,您需要为列表中的每个元素找到 lastChild。

$x("//div[contains(@class, 'pb-basket-item-price')]")[1].lastChild -> 499 TL
$x("//div[contains(@class, 'pb-basket-item-price')]")[2].lastChild -> 748 TL

现在您已经将所有内容准备好,尝试将此逻辑放入代码中。

英文:

So the idea behind is,

you first need to figure out the elements and convert them to list.

$x(&quot;//div[contains(@class, &#39;pb-basket-item-price&#39;)]&quot;)

Once you have the list in place, you need to find the lastChild for each element in the list.

$x(&quot;//div[contains(@class, &#39;pb-basket-item-price&#39;)]&quot;)[1].lastChild -> 499 TL
$x(&quot;//div[contains(@class, &#39;pb-basket-item-price&#39;)]&quot;)[2].lastChild -> 748 TL

Now you have everything in place, try putting this logic in code.

huangapple
  • 本文由 发表于 2020年9月4日 15:48:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/63736976.html
匿名

发表评论

匿名网友

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

确定