I am working on a program for hours now, any I am totaly confused. So the deal is that I want to make a steam price lister. Here is the website .
Let's say I want to list the two highest priced item's name and it's price. If you look at the html source you will see that the first 2 found item are named like this:
id=result_0_name
id=result_1_name
But inside the result 0 and result 1 the price just called: "market_table_value"
And I am trying to print out like this:
System.out.println(driver.findElement(By.id("result_0_name"))).getAttribute("market_table_value");
But then I realised that the program won't recognise that I want the "result0"'s price, maybe it will always give me a static number, because there is no unique number for the market value.
Sorry if I confused you guys, but I really hope that you will understand me, because my mind got blown up, and I have no idea how to do it :/
You want to use xPath here:
//*[@id="result_0_name"]
//*[@id="result_0"]/div[1]/span[1]/span
Java code
public class Item {
private String name;
private String price;
public Item(String name, String price) {
this.name=name;
this.price=price;
}
public String getName() {
return name;
}
public String getPrice() {
return price;
}
}
/**
*
* Get item at position {@code position} or {@code null} if not found.
*
*/
private Item getHighestItem(int position, WebDriver driver) {
Item item=null;
try {
int index = position-1;
String name = driver.findElement(By.xPath("//*[@id='result_" + index + "_name']")).getText();
String price = driver.findElement(By.xPath("//*[@id='result_" + index + "']/div[1]/span[1]/span")).getText();
item = new Item(name, price);
} catch(NoSuchElementException nse) {
// handle exception here...
}
return item;
}
Sample usage
WebDriver driver = ...;
...
Item firstItem = getHighestItem(1, driver); // get Most expensive item
Item secondItem = getHighestItem(2, driver); // get 2nd Most expensive item
...
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.