1

I was trying to scrape some data from a site.I'm using a selenium for it but i am getting NoSuchElementException when i try to click or get data from the elements in the site even though the element present in there.

code


    from selenium import webdriver
    from webdriver_manager.chrome import ChromeDriverManager
    from selenium.webdriver.common.by import By
    
    driver = webdriver.Chrome(ChromeDriverManager().install())
    driver.get('https://www.priceking.com/')
    driver.maximize_window()
    driver.find_element(By.XPATH, "/html/body/div/center/font/a").click()
    # driver.find_element(By.CSS_SELECTOR, "body > div > center > font > a").click()

Here is site link that i was using priceking

i don't know why i can't locate any of the elements in the site.

Prophet
  • 32,350
  • 22
  • 54
  • 79
Abhijith Ea
  • 115
  • 11

4 Answers4

2

The desired element is within a <frame> so you have to:

  • Induce WebDriverWait for the desired frame to be available and switch to it.

  • Induce WebDriverWait for the desired element to be clickable.

  • You can use either of the following Locator Strategies:

    • Using PARTIAL_LINK_TEXT:

      driver.get("https://www.priceking.com/")
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//frame[@name='contents']")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "Show More"))).click()
      
    • Using XPATH:

      driver.get("https://www.priceking.com/")
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//frame[@name='contents']")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(., 'Show More')]"))).click()
      
  • 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
    
  • Browser Snapshot:

ShowMore

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
1

The element Show More is in an iframe. Need to switch to frame to interact with the element.

# Imports required
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver.get("https://www.priceking.com/")

wait = WebDriverWait(driver,30)

# Switch to Iframe
wait.until(EC.frame_to_be_available_and_switch_to_it((By.NAME,"contents")))

show_more = wait.until(EC.element_to_be_clickable((By.XPATH,"//a[contains(text(),'Show More')]")))
show_more.click()

# Can also use Link text to click on the element.
# show_more = wait.until(EC.element_to_be_clickable((By.LINK_TEXT,"Show More")))
# show_more.click()

# switch to default content
driver.switch_to.default_content()
pmadhu
  • 3,373
  • 2
  • 11
  • 23
1

Please use some wait statements before clicking the link and also the element is inside an iframe so switch to frame first then click on the element.

 WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.NAME,"contents")))
 WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//a[text()=' Show More ']")))
 driver.find_element(By.XPATH, "//a[text()=' Show More ']").click()

and always write reliable xPath

Nandan A
  • 2,702
  • 1
  • 12
  • 23
1
  1. That element is inside a frame, you need to switch to that frame in order to access that element.
  2. You have to add some wait / delay before accessing the element.
  3. The locator should be improved.
    Try this:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
    
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get('https://www.priceking.com/')
wait = WebDriverWait(driver, 20)
driver.maximize_window()
wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//frame[@name='contents']")))
wait.until(EC.visibility_of_element_located((By.XPATH, "//a[contains(text(),'Show More')]"))).click()
Prophet
  • 32,350
  • 22
  • 54
  • 79