1

I have this line of code which I am trying to use to obtain the status of an item. Here is the line of code:

item_status = driver.findElement(By.className("status-info")).getText();

I'm not sure how I can adjust this to retrieve the text seen here:

enter image description here

from selenium import webdriver

from selenium.webdriver.chrome.options import Options

from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.common.by import By

from selenium.webdriver.support import expected_conditions as EC


options=Options()
driver=webdriver.Chrome(options=options)

#Directing to site
driver.get("https://www.amazon.co.uk/Nintendo-Switch-OLED-Model-Neon/dp/B098TNW7NM/ref=sr_1_3?keywords=Nintendo+Switch&qid=1651147043&sr=8-3");
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[2]/span/form/div[3]/span[1]/span/input"))).click()
    

2 Answers2

0

driver.findElement(By.className("status-info")) is the Java syntax and getText() is a Java method. Possibly you need Python syntax and method.


Solution

To print the text In stock. you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "span.a-size-base.a-color-success.a-text-bold"))).text)
    
  • Using XPATH:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[@class='a-size-base a-color-success a-text-bold']"))).get_attribute("innerHTML"))
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I tried the XPATH code but I get an error unfortunately. I've now included the full code at the top if you'd like to try, –  Apr 28 '22 at 00:34
0

When you are doing this

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[1]/header/div/div[1]/div[2]/div/form/div[3]/div/span/input"))).click()

it will click on search icon, now on the result page, this xpath //span[@class='a-size-base a-color-success a-text-bold'] is not present hence nothing is getting printed on the console you are likely to face TimedoutException.

However looking at the screenshot that you've shared, I would say to use this xpath

//div[@id='availability']//span[contains(text(),'In stock.')]

If you want to print the text and tag

print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@id='availability']//span[contains(text(),'In stock.')]"))).get_attribute("innerHTML"))

If only text you want:

print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@id='availability']//span[contains(text(),'In stock.')]"))).get_attribute("innerText"))
cruisepandey
  • 28,520
  • 6
  • 20
  • 38
  • I've just tried both code but both are giving me TimeoutException errors unfortunately. –  Apr 28 '22 at 10:07
  • Where exacly `In stock` is present, I could not locate that on UI – cruisepandey Apr 28 '22 at 10:35
  • Apologies, I was using the wrong URL. I updated the code in the question so that I don't confuse others. Your code worked. Thank you very much. –  Apr 28 '22 at 12:02
  • Any idea how I could loop this code?: https://stackoverflow.com/questions/72031026/how-can-i-adapt-this-selenium-code-so-that-it-constantly-refreshes-in-a-loop-for?noredirect=1#comment127280098_72031026 –  Apr 28 '22 at 12:05
  • I will have a look and see if I can help you there ! – cruisepandey Apr 28 '22 at 12:18