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.
Related
I am using Python & Selenium to automate the sending request to add new connections in linkedin. Currently, I have the following problem:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//ul[contains(#class, 'reusable-search__entity-results-list list-style-none')]"}
(Session info: chrome=97.0.4692.99)
Here is the element
< ul class="reusable-search__entity-result-list list-style-none">
Following is the script
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Chrome(executable_path="E:\Softwares\chromedriver\chromedriver.exe")
driver.get('https://www.linkedin.com/')
print(driver.title)
print(driver.current_url)
#Login ID /Email Id
user = driver.find_element_by_xpath('//*[#id="session_key"]').click()
driver.execute_script("document.getElementById('session_key').value='LinkediN-Email'")
#password
driver.find_element_by_xpath('//*[#id="session_password"]').click()
driver.execute_script("document.getElementById('session_password').value='LinkedPassword'")
#signin button click
driver.find_element_by_xpath('//*[#id="main-content"]/section[1]/div/div/form/button').click()
#destination URL
driver.get('https://www.linkedin.com/search/results/people/?origin=CLUSTER_EXPANSION&sid=bl!')
time.sleep(1)
print(driver.title)
print(driver.current_url)
time.sleep(1)
#Error in SkillSelection find element by XPath unable to find the class
skillsSection = driver.find_element_by_xpath("//ul[contains(#class, 'reusable-search__entity-results-list list-style-none')]")
print(skillsSection)
#working code iterating through list and printing elements
for li in skillsSection.find_elements_by_tag_name("li"):
#print(li.text)
Connect_btn_contents = driver.find_elements_by_class_name('artdeco-button__text')
the_one_you_want = [x for x in Connect_btn_contents if "Connect" == x.text][0]
the_one_you_want.click()
time.sleep(4)
Send_btn = driver.find_element_by_xpath("//span[text()='Send']")
print(Send_btn)
Send_btn.click()
driver.close()
What can be the Issue and the correct solution of the issue?
I am new to web scraping and I am trying to scrape reviews off amazon.
After going on a particular product's page on amazon I want to click on the 'see all reviews' button. I did inspect element on the page, I found that the see all reviews button has this structure
structure
So I tried to find this element using the class name a-link-emphasis a-text-bold.
This is the code I wrote
service = webdriver.chrome.service.Service('C:\\coding\\chromedriver.exe')
service.start()
options = webdriver.ChromeOptions()
#options.add_argument('--headless')
options = options.to_capabilities()
driver = webdriver.Remote(service.service_url, options)
driver.get(url)
sleep(5)
driver.find_element_by_class_name('a-link-emphasis a-text-bold').click()
sleep(5)
driver.implicitly_wait(10)
But this returns me the following error
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".a-link-emphasis a-text-bold"}
What am I doing wrong here?
driver.find_element_by_class_name('a-link-emphasis.a-text-bold').click()
By class expects single class not multiple but you can use the above syntax , remove space with . as it uses css under the hood, or use :
driver.find_element_by_css_selector('.a-link-emphasis.a-text-bold').click()
driver.find_element_by_css_selector('[class="a-link-emphasis a-text-bold"]').click()
I'm using selenium to do some web scraping, and I am having trouble getting the program to click on a certain button. Here is the specific error message:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element
I am not sure what is wrong (I have tried these ways to try navigating to the button including:
driver.find_element_by_name("HTML-NAME").click()
driver.find_element_by_class("HTML-CLASS").click()
driver.find_element_by_id("HTML-ID").click()
driver.find_element_by_xpath("//a[#id='HTML-ID']").click()
driver.find_element_by_link_text("HTML-LINK-TEXT").click()
Here is the corresponding HTML code to the button I am trying to click:
< a name="HTML-NAME" id="HTML-ID" ptlinktgt="CODE"tabindex="30" onclick="javascript:cancelBubble(event);" href="javascript:submitAction....." class="HTML-CLASS">BTN-TEXT </a
# My code right now
driver = webdriver.Chrome(executable_path="path/chromedriver")
url = "https://...."
driver.get(url)
time.sleep(5)
input_bar1 = driver.find_element_by_xpath("//input[#id='inp1']")
input1 = input("Username: ")
input_bar1.send_keys(username)
input_bar2 = driver.find_element_by_xpath("//input[#id='inp2']")
input2 = input("Password: ")
input_bar2.send_keys(password)
input_bar2.submit()
time.sleep(5)
elem = driver.find_element_by_link_text("BTN-TEXT")
elem.click()
time.sleep(20)
I would recommend you to increase the waiting time after the submit.
Probably the Browser has to load something (after the submitting) and you are trying to click the element before it is generated in the DOM.
(TIPP: the number in wait(number) is the time in millis. So you are waiting 5 milliseconds before you try to click the BTN-TEXT element. Try increasing this number.
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\"]'
Set-up
I'm trying to log in to a website using Python + Selenium.
My code to load the website is,
browser = webdriver.Firefox(
executable_path='/mypath/to/geckodriver')
url = 'https://secure6.e-boekhouden.nl/bh/'
browser.get(url)
Problem
Selenium cannot locate the element containing the account and password fields.
For example, for the field 'Gebruikersnaam',
browser.find_element_by_id('txtEmail')
browser.find_element_by_xpath('//*[#name="txtEmail"]')
browser.find_element_by_class_name('INPUTBOX')
all give NoSuchElementException: Unable to locate element.
Even worse, Selenium cannot find the body element on the page,
browser.find_element_by_xpath('/html/body')
gives NoSuchElementException: Unable to locate element: /html/body.
I'm guessing something on the page is either blocking Selenium (maybe the 'secure6' in the url) or is written in a language/form Selenium cannot handle.
Any suggestions?
All elements are inside the frame. So that, it is throwing No Such Element exception. Please try to switch to the frame before all actions as given below.
browser = webdriver.Firefox(
executable_path='/mypath/to/geckodriver')
url = 'https://secure6.e-boekhouden.nl/bh/'
browser.get(url)
browser.switch_to.frame(browser.find_element_by_id("mainframe"))
browser.find_element_by_id('txtEmail')
browser.find_element_by_xpath('//*[#name="txtEmail"]')
browser.find_element_by_class_name('INPUTBOX')