Selenium can't find elements even if they exist - python

I'm creating a simple selenium script to enter username and password to log in. Here is my code:
driver = webdriver.Chrome(executable_path=r'C:\\Users\\Aspire5\\Downloads\\chromedriver_win32\\chromedriver.exe')
driver.get("https://ven02207.service-now.com/")
username = driver.find_element_by_xpath('//*[#id="user_name"]')
username.send_keys('username')
password = driver.find_element_by_xpath('//*[#id="user_password"]')
password.send_keys('this_is_password')
But I'm getting following exception:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id="user_name"]"}
I'm accessing this website from code. The XPath I've provided in code are exists on the page, but still it's returning No Such Element Exception.
What I'm missing here? I've seen this, this questions for this, but couldn't find exact answer.

you need to first switch to the frame.. since the input tag is inside the frame
frame = driver.find_element_by_xpath('//*[#id="gsft_main"]')
driver.switch_to.frame(frame)
driver.find_element_by_id('user_name').send_keys('sarthak')
driver.find_element_by_id('user_password').send_keys('sarthak')

You need to wait for element to present in DOM. So try to wait before getting web element
driverwebdriver.Chrome(executable_path=r'C:\Users\Aspire5\Downloads\chromedriver_win32\chromedriver.exe')
driver.get("https://ven02207.service-now.com/")
username = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "//*[#id="user_name"]"))
username.send_keys('username')
password = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, '//*[#id="user_password"]'))
password.send_keys('this_is_password')

Just use escape slashes and you'll be fine
'//*[#id=\"user_name\"]'
'//*[#id=\"user_password\"]'

Related

Select the first result after name search in linked in in python

I am trying to search a name in linked in and then select the first result, I have done almost everything except selecting the first result, I am unable to do that, i have tried with xpath(its only working for the profile u copy xpath not for different profile), but its not working, below is the code what I have tried. For now I am trying for only one profile, but it should work for many profiles. So I need a better solution. Please help.
driver = webdriver.Chrome(r'your chromedriver path\chromedriver.exe')
driver.get("linked_in sign in page url")
username = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='session_key']")))
password = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='session_password']")))
# enter username and password
username.clear()
username.send_keys("give your user name")
password.clear()
password.send_keys("your password")
# target the login button and click it for login
button = WebDriverWait(driver, 2).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[type='submit']"))).click()
# find the search bar and enter name for search
search = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[placeholder='Search']")))
search.clear()
search.send_keys("name of person")
search.send_keys(Keys.ENTER)
# select the first result after search
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,'//*[#id="main"]/div/div/div[1]/div/a/div'))).click()
Instead of that crazy xpath, it works quite well for me to simply use the following: $$('a.app-aware-link')
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'a.app-aware-link'))).click()

Error occurs using Selenium to logging into WSJ

I am trying to write a program to automatically log into WSJ.com with my own login. I've looked at some of the other guy's codes, but none of them seemed to have the problem where when click the Login link on the WSJ.com main page, it takes you to two potential pages:
Ask for user name and password.
Ask for user name, once hit continue brings you to the page mentioned above.
For the 2nd scenario, I have managed to input username and password, then hit login. However that brings me to the next page where it askes if I want to verify my email or continue to WSJ.
In my code, I can't seem to get the webdriver to locate the button that says "Continue to WSJ". If I hit F12 under developer tools, I can locate the button.
Code below:
try:
submit_button = driver.find_element_by_xpath(".//button[#type='button'][#class='solid-button continue-submit new-design']")
username = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, 'username')))
user1 = "username"
pass1 = "password"
username.send_keys(user1)
submit_button = driver.find_element_by_xpath(".//button[#type='button'][#class='solid-button continue-submit new-design']")
submit_button.click()
password = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, 'password')))
password = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, 'password')))
username.send_keys(user1)
password.send_keys(pass1)
driver.get(url)
submit_button = driver.find_element_by_xpath(".//button[#type='button'][#class='solid-button new-design basic-login-submit']")
submit_button.click()
except:
username = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, 'username')))
password = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, 'password')))
user1 = "username"
pass1 = "password"
username.send_keys(user1)
password.send_keys(pass1)
submit_button = driver.find_element_by_xpath(".//button[#type='submit'][#class='solid-button basic-login-submit']")
submit_button.click()
continue_button = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.CLASS_NAME, "solid-button reg-rtl-btn")))
continue_button.click()
For my code when exception occurs, especially trying to locate the "solid-button reg-rtl-btn" button, it gives me the below message:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".solid-button reg-rtl-btn"}
When stringing together classes in a class name selector, they need to get separated by ., which says the next thing coming is also a class name (the first one is automatically a class because you are searching by class).
Thus solid-button.reg-rtl-btn will locate by class name and .solid-button.reg-rtl-btn would locate by CSS selector, but having the space in between meant it did not know how to interpret reg-rtl-button. There are more complicated ways to do this, too, but I do not see why you would want to (maybe if you were worried about the order in which the classes could appear).

Selenium Unable to Locate Element of "Compose Email" Button

I have been using Python to send Gmails with good progress. Unfortunately, Selenium is having problems identifying the "compose" button that allows a user to write and send an email to people.
from selenium import webdriver
your_email = input("Email:")
your_password = input("Password:")
if "#cps.edu" in your_email:
your_email_two = your_email.replace("#cps.edu","")
driver = webdriver.Chrome("C:/Users/Shitty Horrible Pc/PycharmProjects/learningpython/pytjom/chromedriver.exe")
driver.implicitly_wait(4)
driver.get("https://gmail.com")
element = driver.find_element_by_id("identifierId")
element.send_keys(your_email)
element = driver.find_element_by_class_name("VfPpkd-RLmnJb")
element.click()
element = driver.find_element_by_id("identification")
element.send_keys(your_email_two)
element = driver.find_element_by_id("ember489")
element.send_keys(your_password)
element = driver.find_element_by_id("authn-go-button")
element.click()
element = driver.find_element_by_class_name("VfPpkd-RLmnJb")
element.click()
driver.maximize_window()
driver.implicitly_wait(20)
element = driver.find_element_by_class_name("T-I T-I-KE L3")
element.click()
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".T-I T-I-KE L3"}
I have tried maximizing the tab, telling Selenium to wait before attempting to locate the element -- all to no avail. I have also looked into other posts above similar issues and not much has helped. Should I try removing the spaces in the class name? Is there anything else I can do?
Picture of Gmail with compose button and the element type + name
I think I solved my issue by using the older HTML version of Gmail.("https://mail.google.com/mail/u/0/h/s32qz81kdosc/?zy=h&f=1")
The code to identify the "Compose Email" element and press it:
element = driver.find_element_by_link_text("Compose Mail")
element.click()
You can add implicit or explicit waits if you want.

Unable to click on element even after finding it- Python seleneium

I have been pained by a load more button for a while. I'm looking to create a loop where I click "load more" on the skills section of Linkedin pages. However, this button is just not consistently being clicked.
I was under the impression that the issue was that the element was not visible on the page. So, I have a segmented scroll, which continues moving down the page until the element is found. But what's baffling is that even though the page is now moving to the right place, the element is not being clicked. No error is being thrown.
I've tried nearly every version of the element location (xpath, class name, css selector, full xpath). Why would the button not be clicked, if it is visible on the page?
Relevant Code:
##log into Linkedin
linkedin_urls=['https://www.linkedin.com/in/julie-migliacci-revent/']
ChromeOptions = webdriver.ChromeOptions()
driver = webdriver.Chrome('C:\\Users\\Root\\Downloads\\chromedriver.exe')
driver.get('https://www.linkedin.com/login?fromSignIn=true&trk=guest_homepage-basic_nav-header-signin')
driver.maximize_window()
WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.NAME, "session_key"))).send_keys("EMAIL")
WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.NAME, "session_password"))).send_keys("PASSWORD")
WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//button[#class='btn__primary--large from__button--floating']"))).click()
linkedin_urls=['https://www.linkedin.com/in/julie-migliacci-revent/', 'https://www.linkedin.com/in/kathleen-meyers-126a7931']
for linkedin_url in linkedin_urls:
driver.get(linkedin_url)
looking_for_element = True
ldmore = ''
while looking_for_element:
elements = driver.find_elements_by_xpath('/html/body/div[7]/div[3]/div/div/div/div/div[2]/main/div[2]/div[6]/div/section/div[2]/button/span[1]')
if len(elements) > 0:
ldmore = elements[0]
ldmore.click()
looking_for_element = False
else:
global_copyright = driver.find_elements_by_css_selector('#globalfooter-copyright')
if len(global_copyright) > 0:
looking_for_element = False
else:
body = driver.find_element_by_css_selector('body')
sleep(5)
body.send_keys(Keys.PAGE_DOWN)
I've not seen a discussion on SO about element issues when the underlying solution is not visibility. The code is designed to stop once the element is located -- and is performing this correctly. But it just isn't clicking on the element. I'm not sure why this is.
Locations I've tried:
absolute xpath:
driver.find_element_by_xpath('/html/body/div[7]/div[3]/div/div/div/div/div[2]/main/div[2]/div[6]/div/section/div[2]/button/span[1]').click()
relative xpath:
//span[contains(text(),'Show more')]
class name:
pv-profile-section__card-action-bar pv-skills-section__additional-skills artdeco-container-card-action-bar artdeco-button artdeco-button--tertiary artdeco-button--3 artdeco-button--fluid" aria-controls="skill-categories-expanded
css:
body.render-mode-BIGPIPE.nav-v2.theme.theme--classic.ember-application.boot-complete.icons-loaded:nth-child(2) div.application-outlet:nth-child(77) div.authentication-outlet:nth-child(3) div.extended div.body div.pv-profile-wrapper.pv-profile-wrapper--below-nav div.self-focused.ember-view div.pv-content.profile-view-grid.neptune-grid.two-column.ghost-animate-in main.core-rail div.profile-detail div.pv-deferred-area.ember-view:nth-child(6) div.pv-deferred-area__content section.pv-profile-section.pv-skill-categories-section.artdeco-container-card.ember-view div.ember-view > button.pv-profile-section__card-action-bar.pv-skills-section__additional-skills.artdeco-container-card-action-bar.artdeco-button.artdeco-button--tertiary.artdeco-button--3.artdeco-button--fluid
UPDATE:
Tried the JS force, it clicked! But threw up the error: selenium.common.exceptions.WebDriverException: Message: unknown error: Cannot read property 'click' of null
if len(elements) > 0:
ldmore = elements[0]
ldmorebtn = driver.find_element_by_xpath('/html/body/div[7]/div[3]/div/div/div/div/div[2]/main/div[2]/div[6]/div/section/div[2]/button/span[1]').click()
#driver.execute_script("arguments[0].checked = true;", ldmore)
driver.execute_script("arguments[0].click();", ldmore)
The suggestion by #Datanovice to use javascript to force a click worked like a charm. Initially, when I had tried to adapt the solution, I received the error selenium.common.exceptions.WebDriverException: Message: unknown error: Cannot read property 'click' of null.
This error was because I had been using EC.element_to_be_clickable. Instead, when I paired the java method with EC.visibility_of_element_located, the click consistently worked.
The code:
ldmore = WebDriverWait(driver, 30).until(EC.visibility_of_element_located((By.XPATH,'xpath')))
driver.execute_script("arguments[0].click();", ldmore)

How to Fix Python Selenium Can't Click Target Element?

My Python Selenium can't click target element, instead seem click element behind target element?
I try to click, or enter text in 'drop-down meun', but I find that I am result in clicking element behind this 'drop-down. I know this is element behind it, because there is advistering area behind, and the result top-up showing the same advistering material. Here is my code:
# info for login
my_email = 'my_email'
my_passcode = 'my_passcode'
email_url = r'https://www.gmx.com/#.1559516-header-navlogin2-1'
# start driver and open url
driver = webdriver.Chrome(chrome_path)
driver.get(email_url)
# input email account
xpath = r'//*[#id="login-email"]'
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, xpath)))
target = driver.find_element_by_xpath(xpath)
actions = ActionChains(driver)
actions.move_to_element(target).perform()
actions.click().send_keys(my_email).perform()
# input passcode and hit 'enter' to login
xpath = r'//input[#id="login-password"]'
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, xpath)))
target = driver.find_element_by_xpath(xpath)
actions = ActionChains(driver)
actions.move_to_element(target).perform()
actions.click().send_keys(my_passcode).send_keys(Keys.ENTER).perform()
It happens to me for some other site when the site appear to have 'two layers' (not sure if I am using the right word). I can process anything on top layer, and only result in activate anything behind it. Many thank when provide solution!!
You don't need to use this ActionChains class, all you need to do can be done using WebElement.send_keys() and WebElement.click()
You don't need to re-find the element after using WebDriverWait as it returns the WebElement in case of success
I fail to see clickign on login button anywhere in your script, you can locate the relevant button using XPath contains() function like:
xpath = r'//button[contains(#class,"login-submit")]'
and then just call click() function on the resulting variable
Example suggested code:
You don't need to use this ActionChains class, all you need to do can be done using WebElement.send_keys() and WebElement.click()
You don't need to re-find the element after using WebDriverWait as it returns the WebElement in case of success
I fail to see clickign on login button anywhere in your script, you can locate the relevant button using XPath contains() function like:
xpath = r'//button[contains(#class,"login-submit")]'
and then just call click() function on the resulting variable
Example suggested code:
# input email account
xpath = r'//*[#id="login-email"]'
target = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, xpath)))
target.send_keys(my_email)
# input passcode and hit 'enter' to login
xpath = r'//input[#id="login-password"]'
target = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, xpath)))
target.send_keys(my_passcode)
xpath = r'//button[contains(#class,"login-submit")]'
target = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, xpath)))
target.click()

Categories