简体   繁体   中英

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

(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>
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!
}

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.

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());
}

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())

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.

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