Home > Mobile >  How could I find this item using selenium by this text in between the span?
How could I find this item using selenium by this text in between the span?

Time:04-28

I am trying to find this item by this text in-between :

enter image description here

I currently have the following code:

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

#Locating item on page
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[contains(., 'Nintendo Switch (OLED Model)')]"))).click()

however there seems to be a mistake here. What would be the correct way of rewriting this?

CodePudding user response:

driver.findElement(By.id("Nintendo Switch (OLED Model)")) is the Java syntax and possibly you need Python syntax.


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 XPATH and innerText:

    element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[text()='Nintendo Switch (OLED Model)']")))
    
  • Using XPATH and contains():

    element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[contains(., 'Nintendo Switch (OLED Model)')]")))
    
  • 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:

David,

This line of code that you've

#Locating item on page
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[contains(., 'Nintendo Switch (OLED Model)')]"))).click()

here xpath //span[contains(., 'Nintendo Switch (OLED Model)')] has 5 entries in HTML-DOM.

How you can verify the same?

Steps to check:

Press F12 in Chrome -> go to element section -> do a CTRL F -> then paste the xpath, you'd see 1 of 5 entries

That's why your code is not working properly. xpath should locate unique entry in HTML-DOM. If it has multiple entry it will always select the first node.

So changing your xpath to //a//span[contains(., 'Nintendo Switch (OLED Model)')] should filter the element to 1 of 3. Now this element will get the click Nintendo Switch (OLED Model) - White.

If your intention is to pick Nintendo Switch (OLED Model) - Neon Blue/Neon Red then use this XPath

//a//span[contains(., 'Nintendo Switch (OLED Model)') and contains(text(),'Neon Blue/Neon Red')]

and same for another web element as well.

Hope this helps.

  • Related