1

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?

  • driver.findElement(By.id("Nintendo Switch (OLED Model)")) is the Java syntax and your tag is showing python, Please update the tag – Akzy Apr 28 '22 at 00:12

2 Answers2

0

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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Hi thank you for your response. I used the XPATH method you shared. I added a .click() at the end. I can't seem to get it to click on the item. Perhaps I am doing something wrong. –  Apr 28 '22 at 00:26
  • I have included the full code at the top now. –  Apr 28 '22 at 00:26
  • any ideas on how i could fix this? –  Apr 28 '22 at 02:05
0

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.

cruisepandey
  • 28,520
  • 6
  • 20
  • 38
  • 1
    Thanks. Works flawlessly. I shortened it slightly into this: element=WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a//span[contains(., 'Nintendo Switch (OLED Model) - Neon Blue/Neon Red')]"))).click() –  Apr 28 '22 at 09:53