I am trying to click a button on a website using selenium.
Here's the html:
<form action="/login/" id="login" method="post" class="form-full-width">
<input data-testid="loginFormSubmit" type="submit" class="btn btn-success btn-large" value="Log in"
tabindex="3">
</form>
Here's some of my code:
if __name__ == "__main__":
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 100)
driver.get ('https://www.url.com/login/')
driver.find_element_by_name('username').send_keys('username')
driver.find_element_by_name('password').send_keys('password')
driver.find_elements_by_xpath("//input[#value='Log in' and #type='submit']")
I have also tried:
driver.find_element_by_value('log in').click()
driver.find_element_by_xpath("//a[#href='https://www.url.com/home/']").click()
However when I run it, it always comes up with this error message:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//a[#href='https://www.url.com/home/']"}
I am new to selenium and web drivers, so any help would be appreciated.
To 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:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[data-testid='loginFormSubmit'][value='Log in']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#data-testid='loginFormSubmit' and #value='Log 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
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
Related
I have to automate account creation for few "Senior Citizen" that live in nursing home. (for free, I'm volunteer)
I'm figuring out how to click on button like this :
<button type="submit" data-uid="id-3" role="button" class="_2CEO5 _2eWEL _3cHWt _2Jtx4 V9Kxm _1s9tX _2eWEL _3cHWt _2eWEL _3cHWt _2Jtx4 _3cHWt _5Qrl3">
<span class="_100nC"><div class="_2EteE _2WXqV _34xiU _1TcN5 _2WXqV _34xiU" style="border-top-color: rgb(255, 255, 255); border-left-color: rgb(255, 255, 255);"></div></span><span class="_2HwQ3">Create an Account</span></button>
I tried with
on chrome browser , righ-click > copy Xpath
driver.find_element_by_xpath('//*[#id="plex"]/div/div/div/div[1]/form/button')
but the Selenium cant find it
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id="plex"]/div/div/div/div[1]/form/button"}
How to click ?
Thank for any hints
The url is: https://www.plex.tv/sign-up/
Navigating directly to their login auth iframe link
from selenium import webdriver
driver = webdriver.Chrome(executable_path=path)
driver.implicitly_wait(10)
driver.get('https://www.plex.tv/sign-up/')
#accept cookies
driver.find_element_by_xpath('/html/body/div[8]/div/div/div[3]/a[2]').click()
#navigate to iframe src
driver.get(driver.find_element_by_id("fedauth-iFrame").get_attribute('src'))
#input email
driver.find_element_by_xpath('//*[#id="email"]').send_keys("email#email.com")
#input password
driver.find_element_by_xpath('//*[#id="password"]').send_keys('password')
#submit
driver.find_element_by_xpath('//*[#id="plex"]/div/div/div/div[1]/form/button/span[2]').click()
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()
driver.get("https://www.plex.tv/sign-up/")
driver.maximize_window()
try:
driver.find_element_by_xpath("/html/body/div[2]/div[3]/a[1]").click()
driver.find_element_by_xpath("/html/body/div[9]/div/div/div/div[4]/div/a").click()
driver.find_element_by_xpath("/html/body/div[9]/div/div/div/div[5]/a").click()
except Exception as e:
print("Nothing to click before submission!")
iframe_el = driver.find_element(By.ID, "fedauth-iFrame")
# iframe_el.get_attribute("src")
try:
driver.get(iframe_el.get_attribute("src"))
# fill the form
element = (
WebDriverWait(driver, 10)
.until(
EC.presence_of_element_located(
(By.XPATH, "/html/body/div[1]/div/div/div/div[1]/form/button")
)
)
.click()
)
print("button is found")
except Exception as e:
print("no element")
finally:
driver.quit()
Instead of targetting the parent BUTTON target the inner SPAN inducing WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[type='submit'][data-uid^='id'] span:nth-of-type(2)"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#type='submit' and starts-with(#data-uid, 'id')]//span[text()='Create an Account']"))).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
I am trying to use python, selenium and xpath to grab the text $0.180 in the following block of HTML,
<div class="wlp-values-div">
<span class="wlp-last-price-span">$0.180</span>
This code is deep into a websites HTML and I tried to use the following Xpath to locate it:
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.get("https://smallcaps.com.au/stocks/?symbol=AWJ")
time.sleep(3)
webdriver.ActionChains(driver).send_keys(Keys.ESCAPE).perform()
ele = driver.find_element_by_xpath("//div[#class='wlp-values-div']/span[#class='wlp-last-price-span']")
But I receive the error:
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//div[#class='wlp-values-div']/span[#class='wlp-last-price-span']"}
Any help would be greatly appreciated
The last price field is within nested <iframe> elements so you have to:
Induce WebDriverWait for the parent frame to be available and switch to it.
Induce WebDriverWait for the child frame to be available and switch to it.
Induce WebDriverWait for the desired element to be visible.
You can use either of the following Locator Strategies:
Using CSS_SELECTOR:
driver.get('https://smallcaps.com.au/stocks/?symbol=AWJ')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "svg[data-name='close']"))).click()
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#wl-summary")))
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe.wl-quote-frame")))
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "span.wlp-last-price-span"))).get_attribute("innerHTML"))
driver.quit()
Using XPATH:
driver.get('https://smallcaps.com.au/stocks/?symbol=AWJ')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[name()='svg' and #data-name='close']"))).click()
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#id='wl-summary']")))
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#class='wl-quote-frame']")))
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[#class='wlp-last-price-span']"))).get_attribute("innerHTML"))
driver.quit()
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
Console Output:
$0.190
You can find a detailed relevant discussion in How To sign in to Applemusic With Python Using Chrome Driver With Selenium
Reference
You can find a couple of relevant discussions in:
Ways to deal with #document under iframe
Switch to an iframe through Selenium and python
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
<iframe class="wl-summary" id="wl-summary" src="https://cloud.weblink.com.au/clients/smallcaps/summary/summary.aspx?symbol=AWJ&" width="100%" height="25" frameborder="0" scrolling="no" allowtransparency="true" style="height: 1077px;"></iframe>
<iframe class="wl-quote-frame" src="quoteFrame.aspx?symbol=AWJ&" scrolling="no" frameborder="0" height="1" style="height: 177px;"></iframe>
It's in a double nested iframe so you have to switch to it. This also has the appropriate waits instead of using time.sleep() and hoping the elements load. It also clicks the popup to get out of the way and prints the spans text.
wait = WebDriverWait(driver, 10)
driver.get("https://smallcaps.com.au/stocks/?symbol=AWJ")
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#tve_editor > div'))).click()
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CLASS_NAME, 'wl-summary')))
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CLASS_NAME, 'wl-quote-frame')))
elem=wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, 'span.wlp-last-price-span')))
print(elem.text)
Import
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
I am having this code
driver.implicitly_wait(300) # I have tried different timers
driver.find_element_by_xpath("(//button[#aria-pressed='false'])[1]").click()
But I get this error:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"(//button[#aria-pressed='false'])[1]"}
However, this exists in my HTML content, like:
<div class="webix_el_box" style="width:90px; height:26px">
<button aria-pressed="false" type="button" class="webix_img_btn" style="line-height:22px;">
<span class="webix_icon_btn fa-lock red" style="max-width:22px;">
</span> Read Only
</button>
</div>
I have also tried to use the XPath expression:
//button[#class='webix_img_btn']//child::span[#class='webix_icon_btn fa-lock red']
Both XPath expressions worked fine in Google Chrome.
In the website I am able to find the button using Ctrl + F and 'Read Only', can I use it in Selenium?
It is not working because of Selenium Driver is one page behind ajax call after large json data download.
To click on the element with text as Read Only you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.webix_el_box > button.webix_img_btn"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='webix_el_box']/button[#class='webix_img_btn' and contains(., 'Read Only')]"))).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 clicking a 'Next' button with Selenium
Selenium in Python: "NoSuchElementException: Message: no such element: Unable to locate element"
Try doing just
driver.implicitly_wait(300)
driver.find_element_by_xpath("//button[#aria-pressed='false']").click()
This will click on the first element that satisfies the requirements.
Here's the website code:
<div class="question-row clearfix " lang="en">
<div class="qquestion qtext ">
five
</div>
</div>
I'm trying to scrape the text (in this example, it's 'five'). Here's my current code:
qtitle = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//*[#id='prompt-row'div/div"))).get_attribute("text")
However when I run it, this error code comes up:
File "C:\Users\lycop\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
That's all it says. Any help on solving this problem would be appreciated.
To print the text five you can use either of the following Locator Strategies:
Using css_selector and get_attribute():
print(driver.find_element_by_css_selector("div.qquestion.qtext").get_attribute("innerHTML"))
Using xpath and text attribute:
print(driver.find_element_by_xpath("//div[#class='qquestion qtext ']").text)
Ideally, to print the innerText of an element you have to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following solutions:
Using CSS_SELECTOR:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "button.save[type='submit'][onclick]"))).get_attribute("innerHTML"))
Using XPATH:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//button[#class='save' and text()='save'][#type='submit' and #onclick]"))).text)
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
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
I have a nested element possibly within <svg> that I can't seems to access
I tried using
driver.find_element(By.CSS_SELECTOR, 'button.login-fake-btn')
and
driver.find_element(By.CSS_SELECTOR, 'login-fake-btn')
and a few others.
HTML structure of nested svg:
<svg class="1">
<div id="2">
<div>
<div class="3">
<div class="4">
<li>
<button class="5" type="button" id="login-fake-btn">
...closing tags
Snapshot of HTML:
I have no success with xpath either.
Error:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"button.login-fake-btn"}
How do I get to a nested svg using a css selector (or xpath, but I understand css to be better)?
It's a <button> element and it's out of the <svg> tag and possibly moving forward you'd invoke click() on it. Hence to locate 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:
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.login-btn.btn-shadow#login-fake-btn[data-testid='login-fake-btn']")))
Using XPATH:
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='login-btn btn-shadow' and #id='login-fake-btn'][#data-testid='login-fake-btn']")))
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