selenium.common.exceptions.NoSuchElementException in Selenium - python

I am trying to make Selenium automatically open a webpage in Chrome.
This is my code...
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.python.org/")
element = driver.find_element_by_link_text("q")
print(element.click())
This is the error...
selenium.common.exceptions.NoSuchElementException
Pls help.

I see there are 2 problems here:
You have to add delay before element = driver.find_element_by_link_text("q") to let the page load.
Preferably you should use visibility_of_element_located expected condition for this.
I see no element with link text equals to q there

Related

Unable to locate "Accept" Button - Selenium - Beginner Web Scraping

I am trying to use Selenium in order to learn different ways of web scraping.
When the code is executed Firefox starts and the "accept cookies" or what ever pops up. I am unable to locate the "accept" button when inspecting the page.
my code so far:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
import pandas as pd
import time
PATH = "C:/Users/myuser/Desktop/Driver/geckodriver.exe"
driver = webdriver.Firefox(executable_path=PATH)
driver.maximize_window() # For maximizing window
driver.get("https://www.immonet.de/")
button_pos = driver.find_element(by=By.CLASS_NAME, value="sc-gsDKAQ fILFKg")
button_pos.click()
print(driver.title)
input = input()
I get the following error: Unable to locate element: .sc-gsDKAQ fILFKg
My thought was locating the button via the inspect tool as follows:
What am I missing or doing wrong? How would i find the right element?
Thanks!
Pat
First of all, to display this url,accepting the cookies is a must but to accept and click on the cookie button isn't a easy task because cookies button is under shadow root (open) selenium and webdriverWait can do nothing on shadow root,so to execute shadow root you need to apply JavaScript querySelector.
#To execute shadow root and accept cookies
driver.execute_script('''return document.querySelector('div#usercentrics-root').shadowRoot.querySelector('button[data-testid="uc-accept-all-button"]')''').click()
Class attribute in the html element can contain multiple classes separated by space. i.e. "sc-gsDKAQ fILFKg", contains two classes, sc-gsDKAQ and fILFKg.
You can user either but both are random and can be changed next time css is recompiled. I recommend to think of xpath using data-testid attribute

Button Click element issue in selenium using python

I am trying to click button(name command page) on web page but i am unable to do so. i am using selenium with python
code:
wait= WebDriverWait(driver,20)
command_page = wait.until(EC.element_to_be_clickable((By.ID,"Button_ID")))
command_page.click()
I have tried by class name also but i am unable to click the element.
Please help me on this.
As an alternative you can use JavascriptExecutor to perfrom click on certain element if Selenium click() method doesn't trigger the action without any Exception.
element = driver.find_element_by_id("etoolbar_toolbarSection_newcommandpagebtn_id")
driver.execute_script("arguments[0].click();", element)
Please try below solution :
WebDriverWait(driver, 20)
both_button=wait.until(EC.element_to_be_clickable((By.XPATH, "//*[contains(text(), 'Command Page')]")))
both_button.click()
I tried this, seems to be working
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("file://c:/cygwin64/home/das2/abcd.html")
element = driver.find_element_by_id("etoolbar_toolbarSection_newcommandpagebtn_id")
element.click()

Selenium not finding an element

I am trying to retrieve an element that I would like to click on. Here's the opening of the website with Selenium in Python:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument('--dns-prefetch-disable')
driver = webdriver.Chrome("./chromedriver", options=chrome_options)
website = "https://www.agronet.gov.co/estadistica/Paginas/home.aspx?cod=4"
driver.get(website) # loads the page
Then, I look for the element I'm interested in:
driver.find_element_by_xpath('//*[#id="cmbDepartamentos"]')
which raises a NoSuchElementException error. When looking at the html source (driver.page_source), indeed "cmbDepartamentos" does not exist! and the text of the dropdown menu I am trying to locate which is "Departamentos:" does not exist either. How can I deal with this?
This should work:
iframe=driver.find_element_by_xpath('//div[#class="iframe"]//iframe')
driver.switch_to.frame(iframe)
driver.find_element_by_xpath('//*[#id="cmbDepartamentos"]').click()
Notes:
The reason for NoSuchElementException error is that the element is
inside an iframe. Unless you switch your driver to that iframe,
the identification will not work.
CTRL + F in the Dev Tools panel, then search for the xpath you
defined in your script is always a good way to rule out issues with
your xpath definition, as cause for NoSuchElementException error (and in your case, the xpath is correct)
You might want to consider adding a WebdriverWait for a complete load of the search area/iframe before attempting to find the "Departamentos" field

Selenium webdriver not able to find the element . Error - Message: element not visible

I am trying to automate packt freebook of each day with selenium in python and cant seem to get to the login form .
This is what I have done till now
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
pkt = "https://www.packtpub.com/packt/offers/free-learning"
chromedriver = webdriver.Chrome()
chromedriver.get(pkt)
elem = chromedriver.find_element_by_class_name("twelve-days-claim")
elem.click()
elem2 = chromedriver.find_element_by_class_name("login-popup")
elem2.click()
Till now this thing works but after that I am not able to locate the correct element , what ever I tried results in "element not visible" when I try to send the keys I have tried the following with out any success .
1. username = chromedriver.find_element_by_id("login-form-email")
2.username = chromedriver.find_element_by_id("email")
3.username = chromedriver.find_element_by_id("email-wrapper")
4.username = chromedriver.find_element_by_class_name("cf")
How do I achieve this . I understand that not all elements are visible at start but here after the elem2.click() this login related buttons should be visible in DOM . Please correct me if I am wrong
I tried with CSS selector and it worked for me. Hope it will help you too. Here what I tried:
chromedriver = webdriver.Chrome()
chromedriver.maximize_window()
chromedriver.get(pkt)
elem = chromedriver.find_element_by_class_name("twelve-days-claim")
elem.click()
elem2 = chromedriver.find_element_by_class_name("login-popup")
elem2.click()
username = chromedriver.find_element_by_css_selector("div[id*='form-login'] [id='login-form-email'] input")
username.send_keys( "username" )
Let me know if this solves the issue.
The only way to avoid an Element not Visible Exception is Explicitly wait in your code till the Element is visible, no matter if you try to find the Element by Xpath, CSS selector or By Id.

Python Selenium Drag and Drop

I know that there are already other related posts but none of them give a complete answer. Bellow is the code for drag and drop which I'm using:
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
url = 'http://www.w3schools.com/html/html5_draganddrop.asp'
driver = webdriver.Firefox()
driver.get(url)
element = driver.find_element_by_id("drag1")
target = driver.find_element_by_id("div2")
ActionChains(driver).drag_and_drop(element, target).perform()
Can you tell me what is wrong with this code?
Later edit:
Found the following example which works:
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
firefox = webdriver.Firefox()
firefox.get('http://www.theautomatedtester.co.uk/demo2.html')
draggable = firefox.find_element_by_class_name("draggable")
droppable = firefox.find_element_by_name("droppable")
dragdrop = ActionChains(firefox)\
.drag_and_drop(draggable, droppable)
dragdrop.perform()
It must be related to the page source (js code?) but I don't know what.
You are trying to drop and drag it's correct . But the actual url is
:http://www.w3schools.com/html/tryit.asp?filename=tryhtml5_draganddrop
and the second thing is the two id's are inside a frame so you must *switch_to_frame* first before perform().
I've tried to get this working as well and it seems that switch_to_frame doesn't seem to help. Some additional research has me thinking that perhaps Selenium WebDriver doesn't fully support HTML 5 drag and drop?
https://code.google.com/p/selenium/issues/detail?id=3604
I'm going to see if I can find a nice jquery drag and drop test page that I can use test the iframe behavior on.

Categories

Resources