Home > Mobile >  How can I adjust this line of selenium code to get the status info of this item?
How can I adjust this line of selenium code to get the status info of this item?

Time:04-28

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");

#Searching and navigating to search page
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[1]/span/form/div[3]/span[1]/span/input"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[1]/header/div/div[1]/div[2]/div/form/div[2]/div[1]/input"))).send_keys("Nintendo Switch")
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()

#Printing item on page

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"))

Unfortunately the last line doesn't print and I get an error.

CodePudding user response:

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
    

CodePudding user response:

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 you 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"))
  • Related