英文:
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 :
<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>
答案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<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!
}
答案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<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.
答案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("//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());
}
答案4
得分: 0
以下是翻译后的内容:
要打印文本,您可以使用以下任一定位策略:
-
748
-
使用
css_selector
和get_attribute()
:print(driver.find_element_by_css_selector("div.pb-basket-item-price").get_attribute("innerHTML"))
-
使用
xpath
和 text 属性:print(driver.find_element_by_xpath("//div[@class='pb-basket-item-price']").text)
-
-
419
-
使用
css_selector
和 textContent:print(driver.execute_script('return arguments[0].lastChild.textContent;', driver.find_element_by_css_selector("div.pb-basket-item-price")).strip())
-
使用
xpath
和 textContent: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
andget_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())
-
答案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("//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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论