简体   繁体   English

如何获取 Selenium 文本但当产品有折扣元素更改时

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

(JAVA) I select random product from the site. (JAVA) 我从网站上随机选择产品。 Sometimes it has discount sometimes it does not.有时有折扣有时没有。

For example:例如:

How can I get 748(product without discount)我怎样才能得到 748(没有折扣的产品)

or How can I get 419 (product with discount)或者我怎样才能得到 419(有折扣的产品)

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:假设:HTML 看起来像:

<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.然后,您可以遍历 Web 元素列表并检查元素应具有的特定文本。

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 748

    • Using css_selector and get_attribute() :使用css_selectorget_attribute()

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

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

    • Using css_selector and textContent :使用css_selectortextContent

       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 :使用xpathtextContent

       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.一旦你有了列表,你需要为列表中的每个元素找到 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 $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.现在您已准备就绪,请尝试将此逻辑放入代码中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM