I am attempting to search a page for an element which prints a price (for that listing) and then increments the integer as to not print the same price again, but the price for the next listing. Here is the code I am using. It is already inside a for loop which I have not written out as it is extensive.
price_int = 1
price = driver.find_element(By.XPATH, "//span[@class='market_table_value'][price_int]")
print(price.text)
price_int =1
#(Loop repeats)
When I replace price_int with a value such as 1 or 2 the correct respective price is returned. I am confused as to why the code is not working. It returns this error when built.
"selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: The result of the xpath expression "/" is: [object HTMLDocument]. It should be an element."
Any help is appreciated, I get the feeling this is a simple fix but I have not been able to find it.
CodePudding user response:
Try this:
price = driver.find_element(By.XPATH, "//span[@class='market_table_value']" price_int)
or
price = driver.find_element(By.XPATH, "//span[@class='market_table_value'][" price_int "]")
CodePudding user response:
You need to pass below way:
price = driver.find_element(By.XPATH, "//span[@class='market_table_value'][{0}]".format(price_int))
In the above line 0 represents the first argument to format the string. Don't change its value, it should be always zero since we are passing only one argument.