Python & Selenium: Unable to find class using x-path - python

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?

Related

Python - Using Selenium to Login to Web

I cannot login to this site with Selenium.
This is the url.
https://www.burn-cycle.com/my-account/pearl-district
What I tried:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import yaml
import time
conf = yaml.full_load(open("login_details.yml"))
my_burn_email = conf["user"]["email"]
my_burn_password = conf["user"]["password"]
driver = webdriver.Chrome()
driver.get("https://www.burn-cycle.com/my-account/pearl-district")
time.sleep(1)
username = driver.find_element(By.XPATH, "//*[#id='USERNAME']")
username.send_keys(my_burn_email)
pw = driver.find_element(By.XPATH, "//*[#id='PASSWORD']")
pw.send_keys(my_burn_password)
login_button = driver.find_element(By.XPATH("//*[#id='liFormWrap']/form[1]/button")).click()
The website loads (slowly) but nothing populates. This is the output:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id='USERNAME']"}
(Session info: chrome=103.0.5060.134)
What am I doing wrong?
You need to wait for the website to load completely so that you can fetch those elements from the webpage, you can achieve this by using implicitly.wait(#amount of second) command right after initializing the web driver.
driver = webdriver.Chrome()
driver.implicitly_wait(15) # gives an implicit wait for 15 seconds
the element is under iframe, you need try this way, first switch into iframe,
see this link
driver.get("https://www.burn-cycle.com/my-account/pearl-district")
time.sleep(5)
iframe = driver.find_element(By.XPATH, "//*[#id='sf-frame']")
# switch to selected iframe
driver.switch_to.frame(iframe)
username = driver.find_element(By.XPATH, "//input[#data-val-required='Username is required']")
username.send_keys("test")

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.

How to make Selenium click on this button?

I am trying to extract all articles on this web page, but i can't make Selenium click on the "Continue" button at the end of the page.
I have tried a lot of different versions, but i'll post just the one, which at least doesn't throw an error...:
import time
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
addr = 'https://www.armani.com/de/armanicom/giorgio-armani/f%C3%BCr-ihn/alle-kleidungsstucke'
options = webdriver.ChromeOptions()
options.add_argument("--enable-javascript")
driver = webdriver.Chrome(options=options)
driver.get(addr)
ContinueButton = driver.find_element_by_xpath("//li[#class='nextPage']")
# gives: No error, but also no effect
# ContinueButton = driver.find_element_by_xpath("/html/body/div[3]/main/section/div[2]/div[1]/ul/li[8]/a/span[2]")
# gives: NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[3]/main/section/div[2]/div[1]/ul/li[8]/a/span[2]"}
#ContinueButton = driver.find_element_by_css_selector(".nextPage > a:nth-child(1)")
# gives: NoSuchElementException: no such element: Unable to locate element:
ActionChains(driver).move_to_element(ContinueButton).click()
time.sleep(5)
Chrome engine is v86, but i have tried (and failed) with Firefox as well.
You want to wait for the element to be clickable and then attempt to click on it:
I
import time
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
addr = 'https://www.armani.com/de/armanicom/giorgio-armani/f%C3%BCr-ihn/alle-kleidungsstucke'
options = webdriver.ChromeOptions()
options.add_argument("--enable-javascript")
driver = webdriver.Chrome(options=options)
driver.get(addr)
ContinueButton = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH, "//li[#class='nextPage']/a")))
ActionChains(driver).move_to_element(ContinueButton).click()
time.sleep(5)
The problem is that you are clicking on the li element.
Your click is received but no action is performed, in order to do so you need to target the a element after the li.
Try this:
ContinueButton = driver.find_element_by_xpath("//li[#class='nextPage']/a")

Select element from a list inside an iframe using python selenium

I would like to write a python Programm which downloads automaticaly historical stock data from a web-page. The correspindent HTML-Code of the Element I would like to select is on the following Picture:
There are two iframes. One is inside the other. I switch to the second iframe, but the element I would like to click can't be found. I get the following error: "Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id=":cu"]"} (Session info: chrome=75.0.3770.100)"
from requests import get
from requests.exceptions import RequestException
from contextlib import closing
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import ctypes # An included library with Python install.
import time
user = ""
pwd = ""
driver = webdriver.Chrome()
driver.get("https://www.dukascopy.com/trading-tools/widgets/quotes/historical_data_feed")
driver.maximize_window()
## Give time for iframe to load ##
time.sleep(1)
# get the list of iframes present on the web page using tag "iframe"
seq = driver.find_elements_by_tag_name('iframe')
print("No of frames present in the web page are: ", len(seq))
#switch to correct iFrame
driver.switch_to_default_content()
iframe = driver.find_elements_by_tag_name('iframe')[1]
driver.switch_to.frame(iframe)
driver.implicitly_wait(5)
elem = driver.find_element_by_id(':cu')
elem.click()
ctypes.windll.user32.MessageBoxW(0, "Test", "Test MsgBox", 1)
driver.close()
If my code would be correct the element "EUR/TRY" in the List would be selected.
There are total 4 iframes.
The table you want to interact with is in iframe[src^='https://freeserv'] and parent iframe is widget-container. One by one you have to switch to it like this :
Code :
wait = WebDriverWait(driver,10)
driver.maximize_window()
driver.get("https://www.dukascopy.com/trading-tools/widgets/quotes/historical_data_feed")
wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, "widget-container")))
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[src^='https://freeserv']")))
check_Box = wait.until(EC.visibility_of_element_located((By.XPATH, "//strong[text()='EUR/TRY']/../preceding-sibling::span/span")))
ActionChains(driver).move_to_element(check_Box).perform()
check_Box.click()

Python: Selenium "no such element" XPath or ID

I am attempting to log into Tessco.com
I have learned this site uses JavaScript, which is why I was unable to locate a form using RoboBrowser.
I am now using Selenium. I have used two methods to enter information into a field. One, using the driver.find_element_by_xpath()as well as driver.find_element_by_id()
Both attempts yield an error.
The code is as follows:
import time
from selenium import webdriver
chrome_path = r"C:\Users\James\Documents\Python Scripts\jupyterNoteBooks\ScrapingData\chromedriver_win32\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)
driver.get("https://www.tessco.com/login")
userName = "FirstName.SurName321123#gmail.com"
password = "PasswordForThis123"
elem = driver.find_element_by_xpath("""//*[#id="userID"]""")
elem = send_keys(userName)
elem = driver.find_element_by_xpath("""//*[#id="password"]""")
elem = send_keys(password)
driver.close()
The error is:
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id="userID"]"}
When I call the element by using ID as such:
elem = driver.find_element_by_id("userID")
elem = send_keys(userName)
elem = driver.find_element_by_id("password")
elem = send_keys(password)
I get:
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="userID"]"}
I was under the impression I inspected the element and used the appropriate names.
Any hints or ideas what I am not doing correctly?
Solution provided below in the comments section.
Code modified to:
import time
from selenium import webdriver
#Webdriver wait functions being introduced to add a delay.
#I was running into problems, with the ID not being found
#add wait for the element to be clickable before trying send keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
chrome_path = r"C:\Users\James\Documents\Python Scripts\jupyterNoteBooks\ScrapingData\chromedriver_win32\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)
driver.get("https://www.tessco.com/login")
userName = "FirstName.SurName321123#gmail.com"
password = "PasswordForThis123"
wait = WebDriverWait(driver, 10)
elem = wait.until(EC.element_to_be_clickable((By.ID, "userID")))
elem.send_keys(userName)
elem = wait.until(EC.element_to_be_clickable((By.ID, "password")))
elem.send_keys(password)
driver.close()
It's likely that the element is not visible at first and that causes the failure. Wait until the element is visible/clickable and then use send keys.
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "userID"))).send_keys(userName)
I guess the element search happens before its loaded. My suggestion would try putting some wait and it should work.

Categories