-1

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

For example:

How can I get 748(product without discount)

or How can I get 419 (product with discount)

When Product has discount The element is :

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

When Other Product doesnt have discount The element is :

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

5 Answers5

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

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

, below is the running log:

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

original string: 499 TL "419 TL"
this is what you want: 419 TL

EDIT

Modify according to the question owner's comments.

Suppose: HTML looks like:

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

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

<div class="pb-basket-item-price">
<span>3.374,34 TL</span>
2.339 TL</div>

code:

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("\"", "") + " TL";
    }

    System.out.println("this is what you need: " + str);
    // str is what you want!
}
Peter Quan
  • 788
  • 4
  • 9
  • This code that you written is printing twice an also I got the following logic error original price: 3.374,34 TL 2.339 TL discount price: 3.374,34 TL 2.339 TL. – Batuhan Bakar Sep 04 '20 at 09:16
  • I need only 2.339TL amount when product has discount. – Batuhan Bakar Sep 04 '20 at 09:18
  • Hi @BatuhanBakar , the answer is based on the description in your question. such as, 1. println() twice is just for explaining the code, so you only have to do is focus on "this is what you want:" 2. Speaking of "logic error original price: 3.374,34 TL 2.339 TL discount price: 3.374,34 TL 2.339 TL", I thought there must be double quotation marks after based on your description – Peter Quan Sep 04 '20 at 09:57
  • @BatuhanBakar The answer has been changed. – Peter Quan Sep 04 '20 at 10:21
  • Thank you very much it worked. But now the error is : expected:<[953] TL> but was:<[ 953 ] TL> – Batuhan Bakar Sep 04 '20 at 10:35
  • The actual value has more whitespaces than expected. How to fix it depends on what you really want to compare: just the value or the whole string. – Peter Quan Sep 04 '20 at 10:50
  • I just want to compare the value. What should I change. The result comes like this 1.199 TL 1.199 TL (I mean with endline) – Batuhan Bakar Sep 04 '20 at 11:04
  • https://stackoverflow.com/questions/63740138/selenium-comparisonfailure-between-2-value I asked question at this link. – Batuhan Bakar Sep 04 '20 at 11:19
0

You need to get the elements first of all like:

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

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

Furk4n
  • 21
  • 7
  • This did not work me by the way. I got this error : Expected condition failed: waiting for presence of element located by: By.xpath: //div[contains(@class,'pb-basket-item-price') (tried for 5 second(s) with 500 milliseconds interval) – Batuhan Bakar Sep 04 '20 at 08:19
  • could you try it again? there was a square bracket missing at the end, sorry. – Furk4n Sep 04 '20 at 08:29
  • java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 I got this error. The website is this : https://www.trendyol.com/sepetim#/basket – Batuhan Bakar Sep 04 '20 at 08:37
0

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("//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());
}
Arundeep Chohan
  • 9,779
  • 5
  • 15
  • 32
0

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("div.pb-basket-item-price").get_attribute("innerHTML"))
      
    • Using xpath and text attribute:

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

    • Using css_selector and textContent:

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

      print(driver.execute_script('return arguments[0].lastChild.textContent;', driver.find_element_by_xpath("//div[@class='pb-basket-item-price']).strip())
      
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

So the idea behind is,

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

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

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

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

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

theNishant
  • 663
  • 5
  • 15