Unable to locate element selenium Python - python

I am trying to fill in a form using this code:
from selenium import webdriver
import time
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--no-sandbox')
browser = webdriver.Chrome( chrome_options=chrome_options)
browser.get('https://www.vwe.nl/Autobedrijf/In-en-verkoop/bpm-calculator-
doorverkoop')
time.sleep(5)
#browser.switch_to_frame(browser.find_element_by_id("_hjRemoteVarsFrame"))
kenteken = browser.find_element_by_id('InputControl_txtKenteken')
kenteken.send_keys('simple text')
I tried to see if there is an iframe but it didn't work.
But it drops an error : Unable to locate element

Your element inside iframe, you need switch it first.
This is the iframe locator: By.CLASS_NAME -> iframe_bpm.
You can use combination WebDriverWait and expected_conditions then utilize frame_to_be_available_and_switch_to_it method to switch.
browser = webdriver.Chrome( chrome_options=chrome_options)
browser.get('https://www.vwe.nl/Autobedrijf/In-en-verkoop/bpm-calculator-doorverkoop')
browser.maximize_window()
WebDriverWait(browser, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CLASS_NAME, 'iframe_bpm')))
kenteken = browser.find_element_by_id('InputControl_txtKenteken')
kenteken.send_keys('simple text')
Following import:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

There is an iframe you need to switch it first.Use WebDriverWait.
driver.get("https://www.vwe.nl/Autobedrijf/In-en-verkoop/bpm-calculator-doorverkoop")
WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,'.iframe_bpm')))
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID,"InputControl_txtKenteken"))).send_keys('text')
use following imports.
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

Related

Selenium Web driver ( driver.find_element(By.XPATH, '')) IS NOT WORKING Python

https://www.espncricinfo.com/player/aamer-jamal-793441
This is the URL and here i am trying to access Full Name "Aamer Jamal". with the help of selenium web driver. But I dont know why it gives
NoSuchElementException
`the code is written below:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium_firefox import Firefox
import time
import pandas as pd
driver = webdriver.Firefox()
#Reach to the Landing page
driver.get('https://www.espncricinfo.com/player/aamer-jamal-793441')
driver.maximize_window()
time.sleep(25)
not_now = driver.find_element(By.ID, 'wzrk-cancel')
not_now.click()
fullname = driver.find_element(By.XPATH, '/html/body/div[1]/section/section/div[4]/div[2]/div/div[1]/div/div/div[1]/div[1]/span/h5')
print(fullname.text)`
Error :
NoSuchElementException: Message: Unable to locate element: /html/body/div[1]/section/section/div[4]/div[2]/div/div[1]/div/div/div[1]/div[1]/span
You have to use WebDriverWait expected_conditions explicit waits, not a long hardcoded pauses. You also have to learn how to create correct locators. Long absolute XPaths and CSS Selectors are extremely breakable. The following code works:
from selenium import webdriver
from selenium.webdriver import ActionChains
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(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 60)
actions = ActionChains(driver)
url = "https://www.espncricinfo.com/player/aamer-jamal-793441"
driver.get(url)
wait.until(EC.element_to_be_clickable((By.ID, 'wzrk-cancel')))
fullname = wait.until(EC.visibility_of_element_located((By.CLASS_NAME, 'ds-text-title-l'))).text
print(fullname)
The output is:
Aamer Jamal

Locate the element

# import webdriver
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import WebDriverException
# create webdriver object
driver = webdriver.Firefox()
url = "https://www.zalando.no/tommy-jeans-tjm-essential-down-vest-vest-court-blue-tob22t06j-k11.html"
driver.get(url)
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,'//*[#id="uc-btn-accept-banner"]'))).click()
driver.find_element(By.XPATH, "/html/body/div[4]/div/div[1]/div/div/div[2]/div[1]/x-wrapper-re-1-6/div/div[2]/button/span").click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,"/html/body/div[9]/div/div[3]/div/form/div/div[4]/div/label/span/div/span"))).click()
I'm trying to create a sneaker bot for fun. When I click to select shoes size I can't locate the element for some reason.
wait.until(EC.element_to_be_clickable((By.XPATH,"(//span[.='XS'])[1]"))).click()
Should work for the XS size if you want since most of the elements seem dynamic.
You are using absolute xpath, please switch to relative path.
Also, it's a good practice to create object of WebDriverWait once and then use the reference in other places in your code.
driver = webdriver.Firefox()
driver.maximize_window()
wait = WebDriverWait(driver, 30)
url = "https://www.zalando.no/tommy-jeans-tjm-essential-down-vest-vest-court-blue-tob22t06j-k11.html"
driver.get(url)
try:
wait.until(EC.element_to_be_clickable((By.XPATH, "//*[#id='uc-btn-accept-banner']"))).click()
except:
pass
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[aria-label='Legg i handlekurven']"))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, "//span[text()='L']"))).click()
Imports:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

About: blank when switching window in selenium with Python

The task is to click on the element, go to a new window and get the url of the new window. But instead of url "about: blank" is displayed. How to fix it?
from selenium import webdriver
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.Firefox(executable_path=r'C:\SeleFire\geckodriver.exe')
url = driver.get('http://the-internet.herokuapp.com/windows')
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, 'a[href="/windows/new"]'))).click()
driver.switch_to.window(driver.window_handles[1])
print(driver.current_url)
driver.quit()
2 possible problems here:
Wait for element visibility, not just presence
Small sleep after the click
Try this:
from selenium import webdriver
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.Firefox(executable_path=r'C:\SeleFire\geckodriver.exe')
url = driver.get('http://the-internet.herokuapp.com/windows')
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'a[href="/windows/new"]'))).click()
time.sleep(1)
driver.switch_to.window(driver.window_handles[1])
print(driver.current_url)
driver.quit()

Unable to select drop down using Python Selenium

I am trying to scrape this website: http://sekolah.data.kemdikbud.go.id/
I want to select "Jenjang" field, "SMA" value. After that, need to click "Cari Sekolah" button
Unfortunately, my code does not work. I manage to select SMA but then can't click "Cari Sekolah" to start the query. Anyone knows how to fix this.
Here is my code:
from selenium import webdriver
from selenium.webdriver import Chrome
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import pandas as pd
import time
from selenium.webdriver.support.ui import Select
option = webdriver.ChromeOptions()
option.add_argument('--incognito')
webdriver = "/Users/rs26/Desktop/learnpython/web/chromedriver"
driver = Chrome(executable_path=webdriver, chrome_options=option)
url="http://sekolah.data.kemdikbud.go.id/"
driver.get(url)
wait = WebDriverWait(driver,15)
select_element = Select(driver.find_element_by_id("bentuk"))
select_element.select_by_value("SMA")
wait.until(EC.element_to_be_clickable((By.XPATH,"//button[text()='Cari Sekolah']"))).click()
Please find below solution to select from custom dropdown
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
# # Solution 1:
driver = webdriver.Chrome(executable_path=r"C:\New folder\chromedriver.exe")
driver.get('http://sekolah.data.kemdikbud.go.id/')
driver.maximize_window()
element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "select2-bentuk-container")))
element.click()
list=WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='select2-search__field']")))
list.send_keys("SMA")
select=WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "select2-results__option")))
select.click()
You can use form button[type=submit] css selector to click.
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"form button[type=submit]"))).click()

Selenium Python Script throws no element error exception even though the x path is right?

from selenium import webdriver
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.common.keys import Keys
driver=webdriver.Chrome()
driver.get("https://paytm.com/")
driver.maximize_window()
driver.find_element_by_class_name("login").click()
driver.implicitly_wait(10)
driver.find_element_by_xpath("//md-input-container[#class='md-default-theme md-input-invalid']/input[#id='input_0']").send_keys("99991221212")
In the above code, I have verified the xpath using fire bug its highlighting the correct element. But when the script run its failing? Can you help me folks?
In selenium each frame is treated individually. Since the login is in a separate iframe element, you need to switch to it first using:
iframe = driver.find_elements_by_tag_name('iframe')[0]
driver.switch_to_frame(iframe)
Before trying to interact with it's elements.
Or in this case, you would wait for the frame to exist, and it would be:
from selenium import webdriver
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()
driver.get("https://paytm.com/")
driver.maximize_window()
wait = WebDriverWait(driver, 10)
wait.until(EC.visibility_of_element_located((By.CLASS_NAME, "login"))).click()
wait.until(EC.frame_to_be_available_and_switch_to_it((By.TAG_NAME, "iframe")))
_input = wait.until(EC.visibility_of_element_located((By.ID,"input_0")))
_input.send_keys("99991221212")
You should try using WebDriverWait to wait until input element visible on the page as below :-
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()
driver.get("https://paytm.com/")
driver.maximize_window()
wait = WebDriverWait(driver, 10)
wait.until(EC.visibility_of_element_located((By.CLASS_NAME, "login"))).click()
#now switch to iframe first
wait.until(EC.frame_to_be_available_and_switch_to_it((By.TAG_NAME, "iframe")))
input = wait.until(EC.visibility_of_element_located((By.ID, "input_0")))
input.send_keys("99991221212")
Hope it helps...:)

Categories

Resources