How to Locate Element Using Selenium Chrome Webdriver in Python Selenium

Unable to locate element using selenium chrome webdriver in python selenium

To locate the search field you can use either of the following Locator Strategies:

  • Using css_selector:

    search = driver.find_element_by_css_selector("input[name='search-search-field'][data-cy-id='search-search-field']")
  • Using xpath:

    search = driver.find_element_by_xpath("//input[@name='search-search-field' and @data-cy-id='search-search-field']")

Ideally, to locate the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    search = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='search-search-field'][data-cy-id='search-search-field']")))
  • Using XPATH:

    search = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='search-search-field' and @data-cy-id='search-search-field']")))
  • 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


References

You can find a couple of relevant discussions on NoSuchElementException in:

  • selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium
  • selenium in python : NoSuchElementException: Message: no such element: Unable to locate element

How to identify this element using selenium and Python?

To click on the element with text as Data you can use either of the following locator strategies:

  • Using link_text:

    driver.find_element(By.LINK_TEXT, "Data").click()
  • Using css_selector:

    driver.find_element(By.CSS_SELECTOR, "a.ui-tabs-anchor[id^='ui-id'][title]").click()
  • Using xpath:

    driver.find_element(By.XPATH, "//a[@class='ui-tabs-anchor' and starts-with(@id, 'ui-id')][text()='Data']").click()

The desired element is a dynamic element, so ideally to click() on the clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using LINK_TEXT:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Data"))).click()
  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.ui-tabs-anchor[id^='ui-id'][title]"))).click()
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='ui-tabs-anchor' and starts-with(@id, 'ui-id')][text()='Data']"))).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


References

You can find a couple of relevant discussions on NoSuchElementException in:

  • selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium
  • selenium in python : NoSuchElementException: Message: no such element: Unable to locate element

Selenium ChromeDriver: Cannot find web element I know exists

you need to switch to the iframe first.

iframe = WebDriverWait(driver, 30).until(EC.presence_of_element_located(
(By.XPATH, "//iframe[@id='myIframe']")))
driver.switch_to.frame(iframe)

then find all the items and print their stock availability like this

items = driver.find_elements(By.XPATH, "//div[@class='order']/div[1]")
for item in items:
print(item.text)

finally switch back to default content to access element outside the iframe

driver.switch_to.default_content()

NOTE: You will need to import the following

from selenium.webdriver.support import expected_conditions as EC

Unable to locate element - selenium webdriver python

To click on the element with text Sign In you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    driver.execute("get", {'url': 'https://freecash.com'})
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.account-item.login-btn span"))).click()
  • Using XPATH:

    driver.execute("get", {'url': 'https://freecash.com'})
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='account-item login-btn waves-effect']//span[text()='Sign In']"))).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

find element inside element selenium python chrome driver

the problem with your code on this line:
element_text = element.find_elements_by_xpath(".//div[@class='WP0F WN0F']//div[@class='WDQY']//ul[@class='WHDR WCQY']").text ist that find elements retunes a list, and a list has no .text attribute.
try iterating over the returned list:

elements = driver.find_elements_by_xpath("//div[@class='WGGO WKFO']//ul//li[@class='WO0F WHHO WI5 WF2F']//div[@class='WP0F WN0F']//div[@class='WDQY']//ul[@class='WHDR WCQY']")
#as a list comprehention
element_text= [element.text for element in elements]
#or as a for loop
element_text=[]
for element in elements:
element_text.append(element.text)

selenium webdriver can not find interactive web elements

The issue here is that all the form is inside an iframe which means you need to actually switch to it before searching for the elements (actually the same will be needed for the next page).
This approach by @undetected Selenium works pretty well:

WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@id='ifrPaginaSecundaria']")))
Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//select[@id='ddlDelegacion']")))).select_by_index(INTEGER EXPUNGED)

although it requires 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
from selenium.webdriver.support.ui import Select

with all of that the full code ends up like this:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support import expected_conditions as EC
import re
import datetime
import time
today=today=datetime.date.today()
driver=webdriver.Chrome(executable_path=r"C:\Users\HP\Downloads\chromedriver_win32\chromedriver.exe")
driver.implicitly_wait(10)
for i in range(3):
try:
driver.get("http://rh.imss.gob.mx/tarjetonjubilados/(S(zh0qyvr1lwrxezeum4tjlypg))/default.aspx")
break
except:
driver.navigate().refresh()
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@id='ifrPaginaSecundaria']")))
Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//select[@id='ddlDelegacion']")))).select_by_index(INTEGER EXPUNGED)
user=driver.find_element(By.ID,"txtUsuario")
user.send_keys(STRING EXPUNGED)
password=driver.find_element(By.ID,"txtContraseña")
password.send_keys(STRING EXPUNGED)
nextpage=driver.find_element(By.ID,"btnIngresar")
nextpage.click()
time.sleep(10)
for i in range(3):
try:
driver.switch_to.default_content()
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@id='ifrPaginaSecundaria']")))
filebutton=driver.find_element(By.ID,"rdoArchivo")
filebutton.click()
break
except:
driver.navigate().refresh()
time.sleep(5)
tarjetonbutton=driver.find_element(By.ID,"rdoTarjeton")
tarjetonbutton.click()
tarjetonchoicebutton=driver.find_element(By.XPATH,"/html/body/table/tbody/tr[6]/td/div/div[3]/div[3]/div/table/tbody/tr[2]/td[3]")
tarjetonchoicebutton.click()
tarjetondate=tarjetonchoicebutton.text
tarjetonredate=re.search(r"(?<=\d{2}\/)\d{2}(?=\/\d{4}\s\-)",tarjetondate)
tarjetonmonth=int(tarjetonredate.group())
thismonth=int(today.month)
if thismonth==tarjetonmonth:
print("download cleared")
downloadbutton=driver.find_element(By.ID,"btnAceptar")
downloadbutton.click()

Selenium webdriver chrome, unable to locate button using CSS selector or XPath

This element is inside an iframe. So before interacting with that element, you will need to switch to that iframe, or else it will not work. You can see in the dom that there is an iframe with the title "Iframe title" available.

driver.get("https://epaper.handelsblatt.com/storefront/11")
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"#sp_message_iframe_648626")))
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//button[@title='ZUSTIMMEN']"))).text)
WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//button[@title='ZUSTIMMEN']"))).click()
sleep(10)
driver.quit()

Imports:

from time import sleep
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

Python Scraping using Selenium

To extract the texts from the Referenzcode column you can use list comprehension and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    driver.get("https://www.amazon.in/")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#twotabsearchtextbox"))).send_keys("protein" + Keys.RETURN)
    print([my_elem.text for my_elem in driver.find_elements(By.CSS_SELECTOR, "span[data-component-type='s-search-results'] span.a-size-base-plus.a-color-base.a-text-normal")])
  • Using XPATH:

    driver.get("https://www.amazon.in/")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#twotabsearchtextbox"))).send_keys("protein" + Keys.RETURN)
    print([my_elem.text for my_elem in driver.find_elements(By.XPATH, "//span[@data-component-type='s-search-results']//span[@class='a-size-base-plus a-color-base a-text-normal']")])
  • Note : You have to add the following imports :

    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
  • Console Output:

    ['Saffola Fittify Whey Protein Peanut Butter Dark Chocolate - 925g | Super Creamy | High Protein - High Fiber | No Trans Fats | 100% Real Peanuts, Brown', 'Saffola Whey Protein Peanut Butter Unsweetened Super Creamy 925g', 'Bigmuscles Nutrition Frotein 26g Refreshing Mango Flavored Hydrolysed Whey Protein Isolate 6g Glutamine 15g EAA Per Serving 0g Sugar Light and Crisp Like Juice (15 Servings, 500 gm)', 'Better Nutrition WPC 1kg | Whey Protein Concentrate For Women & Men | No Added Sugar | 24g Protein & 5.8g BCAA per scoop | Unflavored | Muscle Building | WPC for Workouts | Bodybuilding | 100% Veg', 'Bigmuscles Nutrition Premium Gold Whey 1Kg Whey Protein Isolate Blend | USA FDA REGD. BRAND | 25g Protein Per Serving [Belgian Chocolate]', 'Bigmuscles Nutrition Essential Whey Protein 1Kg [Dutch Chocolate] | 24g Protein per serving with Digestive Enzymes, Vitamin & Minerals, No Added Sugar | Improved Strength , Faster Recovery & Muscle Building', 'Bigmuscles Nutrition Crude Whey 1kg | US FDA REGD. BRAND | 24g Protein, 5.5g BCAA, 4 g Glutamine', 'AS-IT-IS Nutrition Whey Protein Concentrate 80% Unflavoured, Labdoor Certified (1kg)', 'Bigmuscles Nutrition Frotein 26g Refreshing Mango Flavored Hydrolysed Whey Protein Isolate [30 Servings, 1 kg] | 15g EAA | 0g Sugar', 'Optimum Nutrition (ON) Gold Standard 100% Whey Protein Powder 2 lbs, 907 g (Double Rich Chocolate), for Muscle Support & Recovery, Vegetarian - Primary Source Whey Isolate', "MuscleBlaze Beginner's Whey Protein, No Added Sugar, Faster Muscle Recovery & Improved Strength (Chocolate, 1 kg / 2.2 lb, 33 Servings)", 'MuscleBlaze Biozyme Performance Whey Protein, (Informed Choice UK, Labdoor USA Certified) with US Patent Filed EAF® (Rich Chocolate, 1 kg / 2.2 lb)', 'Optimum Nutrition (ON) Gold Standard 100% Whey Protein Powder 1 lbs, 454 g (Double Rich Chocolate), for Muscle Support & Recovery, Vegetarian - Primary Source Whey Isolate', 'MuscleBlaze Raw Whey Protein Concentrate 80% with Added Digestive Enzymes, Labdoor USA Certified (Unflavoured, 1 kg / 2.2 lb, 33 Servings)', 'AS-IT-IS ATOM Whey Protein 1kg with Digestive Enzymes | Double rich chocolate | 27g protein | 5.7g BCAA | Lab Tested', 'Cadbury Chocolate Health Drink - Bournvita, 2 Kg', 'Horlicks Lite Health & Nutrition Drink For Adults 450 g Jar, High Protein & Nutrients For Immunity - No Added Sugar (Regular Malt)', 'Nestle EVERYDAY Dairy Whitener, Milk Powder for Tea, 1Kg Pouch', '', '', 'Optimum Nutrition (ON) Gold Standard 100% Whey Protein Powder - 5 lbs, 2.27 kg (Double Rich Chocolate), Primary Source Isolate,For Men and Women', 'NAKPRO PLATINUM 100% Whey Protein Isolate | 28.1g Protein, 6.4g BCAA | Easy Mixing, Low Carbs, Easy Digesting Whey Protein Supplement Powder for Men, Women & Athletes | 1 Kg Chocolate Flavour (30 Servings)', 'NAKPRO Perform Whey Protein Concentrate, Whey Protein Concentrate Supplement Powder (Chocolate, 1 Kg)', 'MuscleBlaze Fuel One Whey Protein, 24 g Protein, 5.29 g BCAA, 4.2 g Glutamic Acid (Chocolate, 1 kg / 2.2 lb)', 'XLR8 Whey Protein with 24 g protein, 5.4 g BCAA - 2 lbs / 907 g (Chocolate Flavour)', 'Bigmuscles Nutrition Crude Whey - 1 kg (Rich Chocolate) | US FDA REGD. BRAND | Whey Protein Concentrate 80%, 24g Protein, 5.5g BCAA, 4 g Glutamine', 'Optimum Nutrition (ON) Gold Standard 100% Whey Protein Powder - 5 lbs, 2.27 kg (Double Rich Chocolate), Primary Source Isolate,For Men and Women', 'Optimum Nutrition (ON) Gold Standard 100% Whey Protein Powder- 4 kg, 129 servings (Vanilla Ice Cream), for Muscle Support & Recovery, Vegetarian - Primary Source Whey Isolate', 'Phab Protein Milkshake with Immunity Boosters – 17g Milk Protein, No added sugar, Vitamin B12 & Calcium Rich: Pack of 24x 200ml (Classic Chocolate)', 'OZiva Protein & Herbs for Women, Chocolate 500g | Natural Protein Powder for Women for Weight Control, Better Metabolism & Hormonal Balance | With Multivitamin for women, 23g Whey Protein, Ayurvedic Herbs, Certified Clean', 'AS-IT-IS Nutrition Whey Protein Isolate 90% - 1 kg | Protein 27g & BCAA 5.9g per serving', 'NAKPRO GOLD 100% Whey Protein Concentrate |Easy Mixing, Low Carbs, Easy Digesting Whey Protein Supplement Powder for Men, Women & Athletes | 1 Kg Unflavored (30 Servings)', 'Protinex Vanilla Delight - 400 g', 'Optimum Nutrition (ON) Performance Whey Protein Powder, 24g Protein, 5g BCAA – 1Kg (Chocolate Milkshake), Whey Protein Blend with Whey Protein Isolate', "MuscleBlaze Beginner's Whey Protein, No Added Sugar, Faster Muscle Recovery & Improved Strength (Chocolate, 1 kg / 2.2 lb, 33 Servings) with Shaker, 650 ml (Combo Pack)", 'NAKPRO PERFORM Whey Protein Concentrate | 24g Protein, 5.7g BCAA | Added Digestive Enzymes | Easy Mixing, Easy Digesting, (Unflavoured, 1 kg (Pack of 1) - Perform)', 'MuscleBlaze 100% Whey Protein, Ultra Premium Whey Blend (Rich Milk Chocolate, 1 kg / 2.2 lb, 30 Servings)', 'Bigmuscles Nutrition Raw & Real Isolate Organic Whey Protein [1Kg] | Natural 90% Isolate Whey Protein | Additives Free | Unflavored | 27g Protein With 5.5g BCAA & 4g Glutamine', 'MuscleBlaze Raw Whey Isolate 90% with Digestive Enzymes (Unflavoured, 1 kg / 2.2 lb, 33 Servings)', "AVVATAR WHEY PROTEIN | 1Kg | Unflavoured | Made with 100% Fresh Cow's Milk", 'Scitron Advance Whey Protein (28.5 Servings, 25.5g Protein, 6g BCAAs, 0g Sugar, 20 Vitamins & Minerals) \x80\x93 1kg (Milk Chocolate)', 'Muscle Asylum- Muscle Whey 100% Whey Protein - 24g Protein, 5.29g BCAA - Strawberry (29 Servings) - 1 Kg (2.2Lbs)', 'Nutrela 100% Whey Performance Protein Powder Supplement with Biofermented Vitamins & Digestive Enzymes, Pre Post Workout - Chocolate Flavour - 1 Kg - 33 Servings', 'PENTASURE - Daily Nutrition Protein Shake- Vanilla Flavour 400gm', 'GNC AMP Pure Isolate Powder Vanilla 1kg', 'Optimum Nutrition (ON) Gold Standard 100% Whey Protein Powder- 5 X 30.4 g Single Serve Sachets (Double Rich Chocolate), for Muscle Support & Recovery, Vegetarian - Primary Source Whey Isolate', 'Scitron Raw Whey (100% Whey Protein Concentrate, 24g Protein, 0g Sugar, 33 Servings, Essential & Non-Essential Amino Acids, No Added Flavour & Sweetener) - 1 kg', 'FAST&UP Essentials Whey Protein Isolate Blend (Rich Chocolate flavour), 30 Servings, 24g Protein, 5.5g BCAA, 4g Glutamine (2.1 Lbs, 960gm)', 'Nakpro Platinum 100% Whey Protein Isolate 30.4g Protein, 6.96g BCAA & 5.38g Glutamine Helps Build Your Muscle Size For Men, Women & Athletes - 1 Kg Unflavoured - (30 Servings)', 'GNC Pro Performance 100% Whey Protein - 2 kg (Chocolate Supreme)', 'HealthOxide My First Protein with whey, casein & pea, Chocolate – 1 kg', 'MuscleBlaze Whey Gold, 100% Whey Protein Isolate, Labdoor USA Certified (Rich Milk Chocolate, 1 kg / 2.2 lb, 33 Servings)', 'Himalaya Quista Pro Advanced Whey Protein Powder - 1kg (Chocolate)', 'MuscleBlaze Raw Whey Protein Concentrate 80% with Added Digestive Enzymes, Labdoor USA Certified (Unflavoured, 2 kg / 4.4 lb, 66 Servings)', 'PRO2FIT Vegan Plant protein powder with Pea protein Brown Rice and Mungbean Protein (Non-GMO, Gluten Free, Vegan Friendly , Non dairy, soy free) for women , men and family , Mint Chocolate 500 gms', 'OZiva Protein & Herbs, Men (23g Whey Protein, 5.5g BCAA & Ayurvedic herbs like Ashwagandha, Chlorella & Musli) for Better Stamina & Lean Muscles, Chocolate, Certified Clean, 500g', 'Abbzorb Whey Isolate (Chocolate) 1kg, 27.4g Protein, 7.04g BCAA per serving with Digestive Enzymes, Guaranteed Lab Report', 'Oxin Nutrition Whey Protein Concentrate Powder | 24.56gm Protein, 5.2gm BCAAs & 4.3gm Glutamine | Whey from USA (Cold Coffee 2 lbs)', 'MuscleBlaze Whey Energy with Whey & Multivitamins Blend (Chocolate, 1 kg / 2.2 lb, 30 Servings)', 'Brave Nutrition Whey Pro Protein Isolate Blend - 24g Protein, 11g EAAs, 4g Glutamine | Lean Protein Powder for Muscle Gain | Sports Nutrition | Muscle Builder Whey Concentrate Protein Powder for Men & Women [1Kg, Chocolate] Free T-Shirt', 'Abbzorb Whey Protein (Chocolate) 1kg, 26.22g Protein, 5.46g BCAA per serving with Digestive Enzymes, Guaranteed Lab Report', 'Nutrabay Pure 100% Whey Protein Concentrate || Raw Whey - Unflavoured, 2 kg', "Nature's Island Pure Whey Protein With Digezyme?, European Whey, High Absorption, No Sugar, No Sucralose, No Thickener, No Protein Spiking - 24G Protein, 5G BCAAs -1Kg, Kesar Kulfi", 'Phab Protein Milkshake with Immunity Boosters €“ 17g Milk Protein, No added sugar, Vitamin B12 & Calcium Rich:Pack of 6x 200ml (Variety Pack)', 'Optimum Nutrition (ON) Gold Standard 100% Whey Protein Powder 2 lbs, 907 g (Double Rich Chocolate), for Muscle Support & Recovery, Vegetarian - Primary Source Whey Isolate']


Related Topics



Leave a reply



Submit