Selenium Webdriver is unable to find any element in a webpage - python

Trying to write a webscraper but I need to get into a website that requires an account. I'm going through 2 factor authentication to get in as that felt more secure than by username and password but I can't get Selenium to find the input for the 2fa code.
I tried every type of locater available and none of those work. The website does not appear to have a frame so I don't think that's the issue. I'm using an implicit wait as well so I believe the webpage has enough time to load.
I'm not really sure what I'm doing wrong at this point. I just keep getting an Unable to locate element error
Here the html for the item
<input class="sc-jzJRlG hxEkIM sc-VigVT gupvKM" data-testid="security-code" id="security-code" value="" autocomplete="off" autocorrect="off" spellcheck="false" maxlength="255" type="password" data-reactid=".0.1.0.2.1.1.0.0.1.0">
and my code
driver.implicitly_wait(10)
codeInput = driver.find_element(By.XPATH, '/html/body/div/div/div[2]/div[1]/div[3]/div/form/div/div[1]/div[2]/input')
enterButton = driver.find_element(By.XPATH, '/html/body/div/div/div[2]/div[1]/div[3]/div/form/div/div[2]/div/button')
codeInput.send_keys('123456')
action.click(enterButton)
action.perform()
and the Stacktrace
Message=Message: Unable to locate element: /html/body/div/div/div[2]/div[1]/div[3]/div/form/div/div[1]/div[2]/input
Stacktrace:
RemoteError#chrome://remote/content/shared/RemoteError.jsm:12:1
WebDriverError#chrome://remote/content/shared/webdriver/Errors.jsm:192:5
NoSuchElementError#chrome://remote/content/shared/webdriver/Errors.jsm:404:5
element.find/</<#chrome://remote/content/marionette/element.js:291:16
Source=C:\Users\user\source\repos\WebScraper\WebScraper\webscraper.py
StackTrace:
File "C:\Users\user\source\repos\WebScraper\WebScraper\webscraper.py", line 27, in <module> (Current frame)
codeInput = driver.find_element(By.XPATH, '/html/body/div/div/div[2]/div[1]/div[3]/div/form/div/div[1]/div[2]/input')

Related

Selenium Python Checkbox Click

I am trying to access the following HTML checkbox for a button click:
<input type="checkbox" data-ng-value="sf.name" data-ng-model="sf.checked" ng-click="ec.onStateFilterChanged(sf)" title="Select a state" class="ng-untouched ng-valid ng-dirty ng-valid-parse" value="Arizona">
using:
state = driver.find_element_by_xpath("input[#type='checkbox']").click()
but keep getting error:
selenium.common.exceptions.NoSuchElementException: Message:
what might be the element path I am looking for in order to select the checkbox?
Your xpath is most likely incorrect - you need to enter // before the element as this will find all (single slash / will work here too though as you are only trying to find one element and it will find the first match)
Try one of the following:
state = driver.find_element_by_xpath("//input[#type='checkbox']").click()
OR
state = driver.find_element_by_css_selector("input[type='checkbox']").click()

Python selenium unable to locate username element

I am trying to create an automated login system for my local library website (http://mcls.ent.sirsi.net/client/en_US/mclweb) using Python and Selenium. However, my script is unable to find the username box.
The HTML from the website for the username box looks like this
<input maxlength="30" class="user_name_input" id="j_username" name="j_username" type="text">
and this is the code I used to find it
username = browser.find_element_by_id('j_username')
username.send_keys(u)
however, I am getting the following error:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"id","selector":"j_username"}
Am I supposed to use a different function? Or did I use find_element_by_id() wrong? Thanks in advance.
The login form lies on a frame. You have to switch to the frame.
browser.get("http://mcls.ent.sirsi.net/client/en_US/mclweb")
browser.find_element_by_class_name('loginLink').click()
time.sleep(5)
browser.switch_to.frame(1) //login iframe is the second frame in the page
time.sleep(5)
browser.find_element_by_id('j_username').send_keys(u)
Alternative way, in case you want to do without switching to frame:
browser.get("http://mcls.ent.sirsi.net/client/en_US/mclweb/search/patronlogin")
browser.find_element_by_id('j_username').send_keys(u)
U can also login to that site without using iframe
browser.get("http://mcls.ent.sirsi.net/client/en_US/login")
browser.find_element_by_id('j_username').send_keys(u)

Unable to send text to Email field on Microsoft login page using Selenium

I'm trying to figure out a way to automatically log in / enter text into a given text field on a particular web page. I've already don't this before, but this particular page isn't responding to anything I've thrown at it yet.
The default page load already has the auto-focus on the necessary text box. I'm currently using Python to write the Selenium code. My current script includes prior processes that lead to the page in question, where my current problem lies. Additionally, I've been running this code in a Google-Chrome browser, but with the user-agent selected to Edge - Mobile (but that probably won't matter here).
The website in question is the Microsoft login at this link.
The CSS/HTML of the text box in question:
<input type="email" name="loginfmt" id="i0116" maxlength="113" lang="en" class="form-control ltr_override" aria-describedby="usernameError loginHeader loginDescription" aria-required="true" data-bind="textInput: usernameTextbox.value,
hasFocusEx: usernameTextbox.focused,
placeholder: $placeholderText,
ariaLabel: tenantBranding.UserIdLabel || str['CT_PWD_STR_Username_AriaLabel'],
css: { 'has-error': usernameTextbox.error },
attr: inputAttributes" placeholder="Email, phone, or Skype" aria-label="Enter your email, phone, or Skype.">
The code I'm currently testing (which is basically three varied iterations of the same idea), after the given page loads:
element = driver.find_element_by_id("i0116")
element.click()
element.clear()
element.send_keys("wbhyatt3#gmail.com")
element.send_keys(Keys.RETURN)
time.sleep(1)
element = driver.find_element_by_name("loginfmt")
element.click()
element.clear()
element.send_keys("wbhyatt3#gmail.com")
element.send_keys(Keys.RETURN)
time.sleep(1)
element = driver.find_element_by_css_selector("input.email")
element.click()
element.clear()
element.send_keys("wbhyatt3#gmail.com")
element.send_keys(Keys.RETURN)
Unfortunately, trying to select the textbox via the input id, class, or name don't seem to be working. It should be worth noting that the page CSS I'm referencing for the text box includes an element "input" prior - I'm not sure if this will affect my current code. I'm fairly certain that either the send_keys aren't working, or perhaps the selection of the element, itself.
What makes the situation even more frustrating is that the page's default focus is on the textbox - so I don't even truly need to select the element, I just need to be able to enter text and submit/enter.
I've also tried targeting it as an iframe, but that hasn't seemed to help either.
Any ideas? Any and all help would be deeply appreciated. I am simply trying to find a way to enter text into the login box.
To enter an EmailID into the field with placeholder text as Email, phone, or Skype you can use the following code block :
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get('https://login.live.com/login.srf')
print("Page Title is : %s" %driver.title)
element = driver.find_element_by_xpath("//input[#class='form-control ltr_override' and #name='loginfmt']")
element.click()
element.clear()
element.send_keys("wbhyatt3#gmail.com")
Console Output :
Page Title is : Sign in to your Microsoft account
Snapshot :

Clicking on an image in selenium

I'm having an issue clicking on an image using the Chromedriver with Selenium for the following HTML:
<div class="modal_buttons">
<input type="image" name="GoButton" id="GoButton" tabindex=14 title="Continue" alt="Continue" onfocus="SetLastFocus(this.id)" src="https://t4.ftcdn.net/jpg/00/81/83/05/240_F_81830511_aJbF2vH9yufF0UAUFQ83JDnbp0jE5mNV.jpg"
I tried using the following code:
element = driver.find_element_by_css_selector("//div[img/#src='https://t4.ftcdn.net/jpg/00/81/83/05/240_F_81830511_aJbF2vH9yufF0UAUFQ83JDnbp0jE5mNV.jpg']").click()
Selenium is failing everytime, and giving the same list of errors that it can't locate the button. Any ideas how to fix?
Thanks
Try:
driver.find_element_by_xpath("//input[#id='GoButton'][#name='GoButton']").click()
or
driver.find_element_by_xpath("//input[#id='GoButton'][#title='Continue']").click()
or
driver.find_element_by_xpath("//input[#name='GoButton'][#title='Continue']").click()
or
driver.find_element_by_xpath("//input[contains(#src,'240_F_81830511_aJbF2vH9yufF0UAUFQ83JDnbp0jE5mNV.jpg')]")

Interacting with website forms

I'm trying to connect to a school url and automate the process with selenium. Originally I tried using splinter, but ran into similar problems. I can't seem to be able to interact with the username and password fields. I realized a little ways in that it is an iframe that I need to interact with. Currently I have:
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("https://my.oregonstate.edu/webapps/login/")
driver.switch_to.frame('Content') #I tried contentFrame and content as well
loginid = driver.find_elements_by_id('user_id')
loginid.send_keys("***")
passwd = driver.find_elements_by_id('password')
passwd.send_keys("***")
sub = driver.find_elements_by_id('login')
sub.click()
time.sleep(5)
driver.close()
Here is the HTML that I am trying to interact with:
The Website: https://my.oregonstate.edu/webapps/portal/frameset.jsp
The iframe:
<iframe id="contentFrame" style="height: 593px;" name="content" title="Content" src="/webapps/portal/execute/tabs/tabAction?tab_tab_group_id=_1_1" frameborder="0"></iframe>
The forms:
Username:
<input name="user_id" id="user_id" size="25" maxlength="50" type="text">
Password:
<input size="25" name="password" id="password" autocomplete="off" type="password">
It seems that selenium can locate the elements just find, but I am unable to input any information into these fields, I got the error 'List object has no attribute'. When I realized it was the iframe I tried to navigate into that but it says 'Unable to locate frame: Content'. Is there another iframe that I am missing? Or something obvious? This is my first time here so sorry if I messed something up with the code linking.
Thanks for the help.
driver.switch_to.frame() takes frame's id or name, where your frame have id = contentFrame and name = content. (The reason they didn't work is probably because of a different issue, read through please)
First, please try use either one of them, not Content (which has upper case C).
Once you have fixed the issue above, there will be another error in your code.
loginid = driver.find_elements_by_id('user_id')
loginid.send_keys("***")
driver.find_elements_by_id finds all matching elements, which is a list. So you can't use send_keys. Please use driver.find_element_by_id('user_id').
Here is the code I tested working.
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("https://my.oregonstate.edu/webapps/login/")
driver.switch_to.frame('content') # all lower case to match your actual frame name
loginid = driver.find_element_by_id('user_id')
loginid.send_keys("***")
passwd = driver.find_element_by_id('password')
passwd.send_keys("***")
Regarding issue in your following comments
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("https://my.oregonstate.edu/webapps/login/?action=relogin")
loginid = driver.find_element_by_id('user_id')
loginid.send_keys("***")
passwd = driver.find_element_by_id('password')
passwd.send_keys("***")
driver.find_element_by_css_selector('.submit.button-1').click()

Categories