Selenium is throwing NoSuchElementException - selenium

I am trying to scrape Myntra website. I have stored all the product links in a list. The code is opening each product link in the list, but when I try to extract any info from the loaded page it is giving me an error. Here's my code,
enter code here
import os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Chrome(r"C:\Users\Sandeep Reddy\Desktop\chromedriver_win32\chromedriver")
options = webdriver.ChromeOptions()
options.add_argument('--disable-notifications')
driver = webdriver.Chrome(executable_path=r"C:\Users\Sandeep
Reddy\Desktop\chromedriver_win32\chromedriver", options=options)
url = 'https://www.myntra.com/men-topwear?p=1&plaEnabled=false'
element = driver.get(url)
#driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
inner_pages_list = []
for i in range(1,51):
inner_document = f'//*[#id="desktopSearchResults"]/div[2]/section/ul/li[{i}]/a'
inner_page = driver.find_elements_by_xpath(inner_document)
for inner_pages in inner_page:
inner_pages_list.append(inner_pages)
product_brand = []
for i in (0,len(inner_pages_list)):
page = inner_pages_list[i]
page.click()
time.sleep(10)
title = driver.find_element_by_xpath('//*
[#id="mountRoot"]/div/div/div/main/div[2]/div[2]/div[1]/h1[1]')
break

Related

Selenium cant find element

<input class="form__input number-verify" type="tel" id="sms-number" maxlength="20">
tel = browser.find_element(By.ID , "form__input number-verify")
It's not working. I want to click that element and then write (ı know how do it) but ı cant solve my problem.
and that all code.
from selenium import webdriver
from bs4 import BeautifulSoup
from selenium.webdriver.common.by import By
import time
browser = webdriver.Edge()
browser.get("https://globfone.com/send-text/")
soup = BeautifulSoup(browser.page_source, 'html.parser')
giris_yap = browser.find_element(By.NAME,"sender-name")
giris_yap.click()
giris_yap.send_keys("abc")
next_button = browser.find_element(By.CLASS_NAME, "cover__btn")
next_button.click()
time.sleep(5)
tel = browser.find_element(By.ID , "form__input number-verify")
time.sleep(5)
browser.close()
Your main mistake is that form__input number-verify is not ID, these are 2 class values, so you can use CSS Selector or Xpath to use these 2 class names.
Also, you should not use hardcoded pauses. WebDriverWait expected_conditions should be used instead.
The following code works:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
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()
options.add_argument("start-maximized")
webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(service=webdriver_service, options=options)
url = 'https://globfone.com/send-text/'
driver.get(url)
wait = WebDriverWait(driver, 20)
giris_yap = wait.until(EC.element_to_be_clickable((By.NAME, "sender-name")))
giris_yap.click()
giris_yap.send_keys("abc")
wait.until(EC.element_to_be_clickable((By.CLASS_NAME, "cover__btn"))).click()
tel = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".form__input.number-verify")))
tel.send_keys("755")
the result is:

How to find href link from multiple browser tabs

I wrote this code to fetch the href link of images in webpage. However i found this way to do this, But Unable to find link in loop. I want that when new tab open href link of that tabs get print in shell. And the urls link are multiple.
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
import urllib
import urllib.request
import time
import os
#driver = webdriver.Chrome(executable_path=r"C:\Users\umesh.kumar\Downloads\Codes\chromedriver.exe")
username = "ABCD.kumar"
password = "X0XX"
driver = webdriver.Chrome()
driver.get("http://propadmin.99acres.com/propadmin/index.htm")
urls = ["https://propadmin.99acres.com/do/seller/ProcessSellerForms/getDeletedPhotos?prop_id=A18056415", "https://propadmin.xxxxxxx.com/do/seller/ProcessSellerForms/getDeletedPhotos?prop_id=A56063622"]
driver.maximize_window()
driver.find_element(By.ID, "username").send_keys(username)
driver.find_element(By.ID, "password").send_keys(password)
driver.find_element_by_name("login").click()
for posts in range(len(urls)):
print(posts)
driver.get(urls[posts])
if(posts!=len(urls)-1):
driver.execute_script("window.open('');")
chwd = driver.window_handles
driver.switch_to.window(chwd[-1])
elems = driver.find_elements_by_tag_name('a')
for elem in elems:
href = elem.get_attribute('href')
if href is not None:
print(href)

Selenium trouble finding button

I have the following code snippet:
enter image description here
I am trying to select the button download in the page:
enter image description here
I am using the following code
from selenium import webdriver
from selenium.webdriver.common.by import By
import datetime
d_ref = datetime.date.today()
driver = webdriver.Chrome('D:\\User\\Download\\chromedriver.exe')
chrome_options = webdriver.ChromeOptions()
prefs = {'download.default_directory' : 'D:\\User\\Download' }
chrome_options.add_experimental_option('prefs', prefs)
driver.get('https://www.anbima.com.br/pt_br/informar/sistema-reune.htm')
# driver.maximize_window()
driver.execute_script("window.scrollTo(0, 320);")
driver.switch_to.frame(0)
# driver.find_element(By.NAME, "Dt_Ref").clear()
# driver.find_element(By.NAME, "Dt_Ref").send_keys(d_ref.strftime('%d%m%Y'))
dropdown = driver.find_element(By.ID, "TpInstFinanceiro")
dropdown.find_element(By.XPATH, "//option[. = 'C F F']").click()
driver.find_element(By.CSS_SELECTOR, "fieldset:nth-child(3) input:nth-child(1)").click()
The CSS-selector fieldset:nth-child(3) is highlighting Financial Instrument and fieldset:nth-child(3) input:nth-child(1) is not highlighting any element in the DOM. Link to refer
Below CSS-selector is highlighting the Download option in the DOM.
fieldset:nth-child(5) input:nth-child(4)
Better to close the Cookie pop-up to interact with other elements. Use Explicit waits.
# Imports
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver.get("https://www.anbima.com.br/pt_br/informar/sistema-reune.htm")
wait = WebDriverWait(driver,20)
# Click on Proceed on the Cookie pop-up
wait.until(EC.element_to_be_clickable((By.XPATH,"//a[#class='LGPD_ANBIMA_global_sites__text__btn']"))).click()
# Switch to Iframe
wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#class]")))
# Select Download option
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"fieldset:nth-child(5) input:nth-child(4)"))).click()

Selenium's find elements using the By is not working for the CSS selector , it is giving empty list

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
chrome_driver_path = YOUR CHROME DRIVER PATH
S = Service(chrome_driver_path)
driver = webdriver.Chrome(service=S)
url = "https://www.python.org/"
driver.get(url)
event_time = driver.find_elements(By.CSS_SELECTOR, ".event-widget last time")
print(event_time)
After printing the event_time , it is giving me empty list
Can somebody help to find this

How to locate button containing text with Selenium?

What I need: switch to the Reviews tab in description of an extension from Chrome Store (e.g. this one) in order to count the number of reviews.
What I've done: Used BeautifulSoup + Selenium to switch between tabs. I used driver.find_element_by_id('id') BUT it returns an error that it can not find the element.
Here's the code I use:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get(url)
button = driver.find_element_by_id(':22')
button.click()
page = requests.get(driver.current_url)
soup = BeautifulSoup(page.content,'html5lib')
comment_list = soup.find('div', class_ = 'e-f-b-L') #the class of reviews I need to count.
Here's the html-code of the Review button element:
Issues:
How do I make it click the 'Reviews' button so the 'Reviews' tab is displayed?
You can click on that Reviews tab very smoothly If you define a simple xpath like '//div[.="Reviews"]' or so. Check out the script as a proof of concept:
from selenium import webdriver
from selenium.webdriver.support import ui
url = "https://chrome.google.com/webstore/detail/emoji-keyboard-by-emojion/ipdjnhgkpapgippgcgkfcbpdpcgifncb?hl=en"
driver = webdriver.Chrome()
wait = ui.WebDriverWait(driver, 10)
driver.get(url)
wait.until(lambda driver: driver.find_element_by_xpath('//div[.="Reviews"]')).click()
driver.quit()
To make it headless:
from selenium import webdriver
from selenium.webdriver.support import ui
url = "https://chrome.google.com/webstore/detail/emoji-keyboard-by-emojion/ipdjnhgkpapgippgcgkfcbpdpcgifncb?hl=en"
chromeOptions = webdriver.ChromeOptions()
chromeOptions.add_argument("--headless")
driver = webdriver.Chrome(chrome_options=chromeOptions)
wait = ui.WebDriverWait(driver, 10)
driver.get(url)
wait.until(lambda driver: driver.find_element_by_xpath('//div[.="Reviews"]')).click()
print("It's done")