Python selenium: unable to locate element on a webpage - python

class PythonOrgSearch(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome() #opens chrome to complete the task
def test_search_in_python_org(self):
driver = self.driver
driver.get(URL) #uses the URL that was generated at the start of the task
self.assertIn("adidas", driver.title)
elem = driver.find_element_by_name("Add to Bag") #finds the 'add to bag' button (for adidas.com) and clicks it
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source
selenium.click("Add to Bag")
def tearDown(self):
self.driver.close()
if __name__ == "__main__":
unittest.main()
Right now i am experimenting with python and trying to make a simple bot for adidas.com that adds a product to the cart. I am using selenium to do so. I try to have selenium click the 'add to bag' button, but when i run i get this error:
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"name","selector":"Add to Bag"}
It cannot find the 'add to bag' button on the website, even though i know its there. what am I doing wrong?
thanks in advance

sometimes I am also facing the same problem by getting element by text.It's best way to get an element by XPath.Hope this XPath will solve your problem
elem =driver.find_element_by_xpath("//*[text()='Add to Bag']")
Hope getting element by XPath will solve your problem

You need to feed the method the id attribute of the element not the text. Looking at the page and the docs it seems that
selenium.click('add-to-bag')
Should get you there.

Related

Selenium locator only works with inspect tab open

I am trying to scrape names and odds for a project I am working on with Selenium 4, and just having an issue with the locator.
When I use the driver.find_element(By.XPATH), the XPATH I give it only seems to work when I open the inspect window on that particular page. When I close the inspect window, it gives me an NoSuchElementException: no such element: Unable to locate element: error.
Code:
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
url = 'https://www.bet365.com.au/#/AC/B13/C1/D50/E2/F163/'
driver.get(url)
Player1 = driver.find_element(By.XPATH, '/html/body/div[1]/div/div[4]/div[3]/div/div/div/div[1]/div/div/div[2]/div/div/div[1]/div[2]/div/div[1]/div[2]/div[1]/div/div[2]/div/div[1]/div').text
Player2 = driver.find_element(By.XPATH, '/html/body/div[1]/div/div[4]/div[3]/div/div/div/div[1]/div/div/div[2]/div/div/div[1]/div[2]/div/div[1]/div[2]/div[1]/div/div[2]/div/div[2]/div').text
Odds1 = driver.find_element(By.XPATH, '/html/body/div[1]/div/div[4]/div[3]/div/div/div/div[1]/div/div/div[2]/div/div/div[1]/div[2]/div/div[2]/div[2]/span').text
Odds2 = driver.find_element(By.XPATH, '/html/body/div[1]/div/div[4]/div[3]/div/div/div/div[1]/div/div/div[2]/div/div/div[1]/div[2]/div/div[3]/div[2]/span').text
print(f'{Player1}\t{Odds1}')
print(f'{Player2}\t{Odds2}')
Run just the section from Player1 onwards with the inspect window open and without it open. Hopefully you'll be able to replicate the issue.
I also ran
try:
if driver.find_element(By.XPATH, '/html/body/div[1]/div/div[4]/div[3]/div/div/div/div[1]/div/div/div[2]/div/div/div[1]/div[2]/div/div[1]/div[2]/div[1]/div/div[2]/div/div[1]/div').text:
print("yay")
except:
print("nay")
and it seemed to show the same situation. The element couldn't be found without the inspect window open. See attached image for where I got the XPATHs from.
Many thanks in advance!

How to click on <a> tag using selenium in python?

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()

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.

Not able to click on a link. I'm getting the error 'Element is not clickable at point'

Can't click link.
I see error
ElementClickInterceptedException: Message: Element is not clickable at
point (116,32) because another element obscures it
My code:
URL = "https://lenta.com/goods-actions/weekly-products/"
driver = webdriver.Firefox()
driver.get(URL)
time.sleep(2)
# ans = driver.find_element_by_link_text("Казань") this link works OK
ans = driver.find_element_by_link_text("Санкт-Петербург") # ERROR
ans.click()
time.sleep(5)
print("go next")
driver.get(URL)
Important code doesn't work only for "Санкт-Петербург"
There are 2 text strings with a value of "Санкт-Петербур" on this page. One is in the overlay; one is in the page header. The script is trying to click the link in the header (but can't because the overlay has focus).
from selenium import webdriver
URL = "https://lenta.com/goods-actions/weekly-products/"
driver = webdriver.Chrome()
driver.get(URL)
ans = driver.find_element_by_link_text("Санкт-Петербург")
print(ans.get_attribute("class"))
#=> link current-store__link js-pick-city-toggle

Selenium PhantomJS unable to find elements on page because page is blank

Whenever I run my python selenium test case I get this error:
NoSuchElementException: Message: {"errorMessage":"Unable to find element with name... etc
^^ I can't locate the username field because the page is not loading.
I am able to return the url and it is the correct url.
Whenever I save a screenshot of the login page, it returns a solid white page. PhantomJS is going to the correct address but not loading the page. It looks like this is only happening with https sites and not http.
import unittest
from selenium import webdriver
browser = webdriver.PhantomJS(service_args=['--ignore-ssl-errors=true', '-- ssl-protocol=any'])
class TestOne(unittest.TestCase):
def setUp(self):
self.driver = browser
self.driver.set_window_size(2000, 1500)
def test_url(self):
driver = self.driver
self.driver.get("https://urlhere")
print driver.current_url
driver.save_screenshot("path/toscreenshot/screenshot1")
driver.implicitly_wait(30)
driver.find_element_by_name("username").clear()
driver.find_element_by_name("username").send_keys("username")
driver.find_element_by_name("password").clear()
driver.find_element_by_name("password").send_keys("password")
driver.find_element_by_name("submit").click()
# End of login
def tearDown(self):
self.driver.quit()
if __name__ == '__main__':
unittest.main()

Categories