Hello
I created few selenium scripts before on python and they are working fine but don't know why it's not working on a website.
I'm trying to click on a element which appear when "DISQUS" is loaded on that website.
Disqus is a comment section on website which loads after website loading.
So I used sleep method but it didn't work so I tried it with python IDLE to execute code line one by one so I can run locate code once Disqus comment section is completely available but still getting same error. "Webdriver unable to locate element"
Here is my code.
import selenium
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
browser = webdriver.Chrome('E:\\ChromeDriver\\chromedriver_win32\\chromedriver.exe')
browser.get('https://www.eloanus.com/customers-review')
dicuss = browser.find_element_by_class_name('textarea')
print(dicuss)
Error, I'm getting.
Traceback (most recent call last):
File "C:\Users\Acer-573G\PycharmProjects\Appium\venv\import selenium.py", line 12, in <module>
dicuss = browser.find_element_by_class_name('textarea')
File "C:\python3.5\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 563, in find_element_by_class_name
return self.find_element(by=By.CLASS_NAME, value=name)
File "C:\python3.5\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 966, in find_element
'value': value})['value']
File "C:\python3.5\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 320, in execute
self.error_handler.check_response(response)
File "C:\python3.5\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"class name","selector":"textarea"}
(Session info: chrome=69.0.3497.100)
(Driver info: chromedriver=2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e),platform=Windows NT 10.0.17134 x86_64)
I attached the screen shot of inspect element (class) from that website.
Inspect element screen shot
I'm new to programming, any help would be appreciated.
There are multiple iframes, so you have to switch to it before you want the interaction.
It's always a good practice if you switch the control of driver to default once you are done.
Code :
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
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(executable_path = r'D:/Automation/chromedriver.exe')
driver.maximize_window()
driver.get("https://www.eloanus.com/customers-review")
wait = WebDriverWait(driver, 20)
visibilty_login = wait.until(EC.visibility_of_element_located((By.XPATH, "//span[text()='Log In']")))
action = ActionChains(driver)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[id$='disqusCommentsHolder']")))
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[title='Disqus']")))
driver.execute_script("window.scrollTo(0, 100)")
wait.until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Join the discussion…']/following-sibling::div[#class='textarea']"))).send_keys("Hi")
driver.switch_to.default_content()
Hope this helps.
To send a character sequence to the Disqus comment section you need to:
Induce WebDriverWait for the desired parent frame to be available and switch to it
Induce WebDriverWait for the desired nested frame to be available and switch to it
Induce WebDriverWait for the desired element to be clickable and you can use the following solution:
Code Block:
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
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_argument('disable-infobars')
browser=webdriver.Chrome(chrome_options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
browser.get('https://www.eloanus.com/customers-review')
WebDriverWait(browser, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#class='disq2disqusCommentsHolder']")))
WebDriverWait(browser, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[starts-with(#id,'dsq-app') and #title='Disqus']")))
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='textarea'][contains(#aria-label,'Join the discussion')]"))).send_keys("Michael Berger")
Browser Snapshot:
Related
New to Python.
Creating smoke test for Checkout process.
Am unable to click the Proceed to Checkout button.
On the Add-Ons page of the Checkout process, I am unable to click the Proceed to Checkout button which allows the user to go to the Attendee Info page of Checkout.
I've tried a few different things. Below is the most basic attempt.
Any guidance would be appreciated.
import time
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome(ChromeDriverManager().install())
username = 'bmendenhall#sans.org'
password = 'Lloydcole2!'
wait = WebDriverWait(driver, 10)
driver.get('https://www.sans.org/')
driver.find_element(By.CSS_SELECTOR, "a[aria-label='Log In']").click()
driver.find_element(By.CSS_SELECTOR, "input[id='username']").send_keys(username)
driver.find_element(By.CSS_SELECTOR, "input[id='password']").send_keys(password)
driver.find_element(By.CSS_SELECTOR, "input[id='regularsubmit']").click()
time.sleep(3)
driver.get('https://registration.sans.org/sans/s/cart/0a62J000000ClOsQAK')
time.sleep(3)
driver.find_element(By.CSS_SELECTOR, "button[title='proceed to checkout']").click()
time.sleep(10)
driver.close()
Error Messaging:
Traceback (most recent call last): File "/Users/BMendenhall/Desktop/Projects/sans-domain-testing/AddtoCartcontinue.py", line 28, in <module>
driver.find_element(By.CSS_SELECTOR, "button[title='proceed to checkout']").click() File "/Library/Python/3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 857, in find_element
return self.execute(Command.FIND_ELEMENT, { File "/Library/Python/3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 435, in execute
self.error_handler.check_response(response) File "/Library/Python/3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 247, in check_response
raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"button[title='proceed to checkout']"} (Session
I don't know why but CSS_SELECTOR has problem even with button[title] but XPATH works for me
find_element(By.XPATH, "//button[#title='proceed to checkout']")
BTW:
first I tested it with find_elements (with s at the end) and it finds two buttons with 'proceed to checkout' - maybe this makes some problem with CSS.
My code:
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as when
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
import time; from datetime import datetime
driver = webdriver.Chrome('/home/wit/PycharmProjects/gmeet-automation/chromedriver')
driver.get("https://classroom.google.com")
element = driver.find_element(By.XPATH, '//*[#id="gfe-main-content"]/section[1]/div/div/div/ul/li[2]/a')
element.click()
wait = webdriver.support.ui.WebDriverWait(driver, 5)
usernameField = driver.find_element(By.ID, 'identifierId')
username = wait.until(when.element_to_be_clickable((By.ID, usernameField)))
#username = webdriver.support.ui.WebDriverWait(driver, 5).until(when.element_to_be_clickable((By.ID, driver.find_element(By.ID, 'identifierId'))))
time.sleep(1)
username.send_keys("xxx)
Here I made an attempt of using the WebDriverWait function to allow time for the text box to be loaded before sending the keys.
But it errored and returned this:
File "/home/wit/PycharmProjects/gmeet-automation/main.py", line 20, in <module>
username = driver.find_element(By.ID, 'identifierId')
File "/home/wit/PycharmProjects/gmeet-automation/venv/git/google-meet-project/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 1244, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "/home/wit/PycharmProjects/gmeet-automation/venv/git/google-meet-project/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 424, in execute
self.error_handler.check_response(response)
File "/home/wit/PycharmProjects/gmeet-automation/venv/git/google-meet-project/lib/python3.10/site-packages/selenium/webdriver/remote/errorhandler.py", line 247, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="identifierId"]"}
(Session info: chrome=96.0.4664.110)
The selector ID is correct, being identifierId, but since it isn't loaded properly the code could not find and input text. How should I correct the function used for waiting the element to load in order for this to work?
You are mixing selector with web element.
Here
usernameField = driver.find_element(By.ID, 'identifierId')
usernameField is web element. While here
username = wait.until(when.element_to_be_clickable((By.ID, usernameField)))
usernameField should be a string.
Also I'd suggest to prefer using visibility_of_element_located when possible instead of element_to_be_clickable
So, to make your code working try change it as following:
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.webdriver.common.by import By
import time; from datetime import datetime
driver = webdriver.Chrome('/home/wit/PycharmProjects/gmeet-automation/chromedriver')
wait = WebDriverWait(driver, 20)
driver.get("https://classroom.google.com")
wait.until(EC.visibility_of_element_located((By.XPATH, '//*[#id="gfe-main-content"]/section[1]/div/div/div/ul/li[2]/a'))).click()
wait.until(EC.visibility_of_element_located((By.ID, "identifierId"))).send_keys("xxx)
I am trying to select 'Female' Radio Button in the webpage
import time
import selenium.common.exceptions
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
driver = webdriver.Chrome(executable_path="C:\Drivers\chrome\chromedriver.exe")
driver.get("https://fs2.formsite.com/meherpavan/form2/index.html?1537702596407")
wait = WebDriverWait(driver, 60)
element = wait.until(EC.element_to_be_clickable((By.ID, "RESULT_RadioButton-7_1")))
driver.execute_script("arguments[0].click();",element)
#element.click()
#driver.find_element_by_id("RESULT_RadioButton-7_1").click()
print(driver.find_element_by_id("RESULT_RadioButton-7_0").is_selected())
print(driver.find_element_by_id("RESULT_RadioButton-7_1").is_selected())
Error:
C:\Users\kkumaraguru\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/kkumaraguru/PycharmProjects/SeleniumProject/RadioButtons.py
Traceback (most recent call last):
File "C:\Users\kkumaraguru\PycharmProjects\SeleniumProject\RadioButtons.py", line 14, in <module>
element = wait.until(EC.element_to_be_clickable((By.ID, "RESULT_RadioButton-7_1")))
File "C:\Users\kkumaraguru\PycharmProjects\pythonProject\venv\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
Process finished with exit code 1
It seems that it times out waiting for an element with ID RESULT_RadioButton-7_1 to be present on the page. I'd open the page yourself to make sure such element is present. You can do this using javascript in the browser's console: document.getElementById("RESULT_RadioButton-7_1"). If this doesn't work then try to debug through the code, and check what HTML Selenium is looking at to make sure is what you expect.
You can do that using JS intervention, Also make sure to maximize the windows size like below :
driver = webdriver.Chrome(driver_path)
driver.maximize_window()
driver.implicitly_wait(30)
driver.get("https://fs2.formsite.com/meherpavan/form2/index.html?1537702596407")
#time.sleep(5)
element = driver.find_element(By.ID, "RESULT_RadioButton-7_1")
driver.execute_script("arguments[0].click();", element)
I want to access google maps with python, but at first you have to click accept cookies button and I don't know why but I keep getting this error:
File "file", line 12, in
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,
'//*[#id="introAgreeButton"]'))).click() File
"C:\Users\gassp\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\selenium\webdriver\support\wait.py",
line 80, in until
raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message:
Here is my code:
from selenium import webdriver
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
driver = webdriver.Chrome('PATH')
url = 'https://www.google.com/maps/'
driver.get(url)
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="introAgreeButton"]'))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="searchboxinput"]'))).send_keys('gostilne')
driver.submit()
According to https://selenium-python.readthedocs.io/api.html :
exception selenium.common.exceptions.TimeoutException(msg=None, screen=None, stacktrace=None)
Bases: selenium.common.exceptions.WebDriverException
Thrown when a command does not complete in enough time.
Hence you either need to increase the time in WebDriverWait(driver, 10) or check if the condition EC.element_to_be_clickable is even fulfillable.
Furthermore, try the following implementation:
element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="introAgreeButton"]')))
element.click()
That fixed a few problems at my end with a similar code structure.
When I tried to access the site I did not found the xpath locator //*[#id="introAgreeButton"] in chrome dev tool. Not sure what element it refers to.
However when I commented that line and added the click event on the search button, I was able to get the results.
Also there is no submit() method available on driver instance.
Below was my trial using the code you have provided -
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="searchboxinput"]'))).send_keys('gostilne')
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//button[#id='searchbox-searchbutton']"))).click()
Output :-
The problem was that it didn't change between the main frame and the accept cookie frame. Here is the soulution code:
from selenium import webdriver
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
driver = webdriver.Chrome('C:\\Users\\gassp\\OneDrive\\Namizje\\Python.projects\\chromedriver.exe')
url = 'https://www.google.com/maps/'
driver.get(url)
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, '//*[#id="consent-bump"]/div/div[1]/iframe')))
agree = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="introAgreeButton"]/span/span')))
agree.click()
#back to the main page
driver.switch_to_default_content()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="searchboxinput"]'))).send_keys('gostilne')
driver.submit()
When I am trying to click the Incident tab as highlighted in Image, it gives me an error. Below is the script.
from selenium import webdriver
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://*****.service-now.com/")
driver.switch_to.frame('gsft_main')
driver.find_element_by_id('user_name').send_keys('*****')
driver.find_element_by_id('user_password').send_keys('*****')
driver.find_element_by_id('sysverb_login').click()
Incident=driver.find_element_by_link_text('Incident')
This is the element I got when copied after Inspecting the page
<span>Incident</span>
This is xpath
//*[#id="concourse_application_dead1309c611228701e2bda7b4252474"]/a/span
Tried both but no success. Gives me below error.`
Traceback (most recent call last):
File "Test1.py", line 10, in <module>
Incident=driver.find_element_by_link_text('Incident')
File "C:\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 428, in find_element_by_link_text
return self.find_element(by=By.LINK_TEXT, value=link_text)
File "C:\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element 'value': value})['value']
File "C:\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Python37\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"Incident"}
find_element_by_link_text() and find_element_by_partial_link_text() works only on <a> tags, your element is a <span>. You need to use xpath for to locate it
incident = driver.find_element_by_xpath('//span[contains(., "Incident")]')
Since you are navigated to another page you should also add some wait
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.common.by import By
wait = WebDriverWait(driver, 10)
incident = wait.until(ec.visibility_of_element_located((By.XPATH, '//span[contains(., "Incident")]')))
The element with text as Incident is actually within a <span> node. Though it is within a parent <a> node, you won't be able to use find_element_by_link_text() or find_element_by_partial_link_text() to locate the element.
However, as the desired element is a dynamic element so to locate and click() on the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
incident = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "[id^='concourse_application_dead']>a>span")))
Using XPATH A:
incident = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Incident']")))
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
Reference
You can find a detailed relevant discussion in:
Selenium “selenium.common.exceptions.NoSuchElementException” when using Chrome