Python WebDriver: find_element() not finding the element - python

I'm learning the very basics of Web Scraping by following Chapter 12 of Automate the boring stuff with Python, but I'm having an issue with the find_element() method. When I use the method to look for an element with the class name 'card-img-top cover-thumb', the method doesn't return any matches. However, the code does work for URL's other than the example in the book.
I have had to make quite a few changes to the code as-written in order to get the code to do anything. I've posted the full code on GitHub HERE, but to summarise:
The book says to use 'find_element_by_*' methods, but these were producing depreciation messages that directed me to use find_element() instead.
To use this other method, I import 'By'.
I also import 'Service' from 'Selenium.Webdriver.Chrome.Service' because Chromedriver doesn't work otherwise.
I also define options with Webdriver.ChromeOptions() that hide certain error messages about a faulty device which apparently you're just supposed to ignore?
I put the code from the book into a function with 'url' and 'classname' arguments so I can test different url's without having to edit the code repeatedly.
Here is the 'business-part' of the code:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
s=Service(r'C:\Users\antse\AppData\Local\Chrome_WebDriver\chromedriver.exe')
op = webdriver.ChromeOptions()
op.add_experimental_option('excludeSwitches', ['enable-logging'])
def FNC_GET_CLASS_ELEMENT_FROM_PAGE(URL, CLASSNAME):
browser = webdriver.Chrome(service = s, options = op)
browser.get(URL)
try:
elem = browser.find_element(By.CLASS_NAME, CLASSNAME)
print('Found <%s> element with that class name!' % (elem.tag_name))
except:
print('Was not able to find an element with that name.')
FNC_GET_CLASS_ELEMENT_FROM_PAGE('https://inventwithpython.com', 'card-img-top cover-thumb')
Expected output: Found <img> element with that class name!
Since the code does work when I look at a site like Wikipedia, I wonder if there have been changes to the html of the page that prevents the scrape from working properly?
Link to the book chapter HERE.
I appreciate any advice you can give me!

You can't pass multiple classes to find_element. Only one can be present. So replace this:
FNC_GET_CLASS_ELEMENT_FROM_PAGE('https://inventwithpython.com', 'card-img-top cover-thumb')
with this:
FNC_GET_CLASS_ELEMENT_FROM_PAGE('https://inventwithpython.com', 'card-img-top')
If you really want to use both classes, then take a look at this answer which explains things in detail.

Related

python: selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: invalid locator

I wanted to write a python code on python 3.10 with selenium to automize the loging in my gmail account. There is something false with Xpath.
The Error that I am getting is selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: invalid locator
Below is my code
from selenium.webdriver.chrome.service import Service
import time
browser = webdriver.Chrome(service=Service("C:/Users/user/Desktop/chromedriver.exe"))
browser.get("https://www.google.de/")
browser.fullscreen_window()
time.sleep(1)
login = browser.find_element("By.XPATH", "//*[#id='gb']/div/div[1]/div/div[1]/a")
login.click()
time.sleep(2)
browser.quit()```
I tried to change the Xpath but it didn't work. I thought that is something false with selenium code then I uninstalled the selenium and installed it again but it didn't work.
Newcomer...
Google first. Selenium has been industry standard since I remember, this has been posted in THOUSAND sites.
In short, "By.XPATH" is pretty wrong. You pass a string there, you has to pass the function.
So, go back to google (or even here- search ...), and find how to import By function in selenium (like you did with selenium) then pass the method like login = browser.find_element(By.XPATH, "//*[#id='gb']/div/div[1]/div/div[1]/a") and you'll fine.
Remember this after ask very common questions like this, you have THOUSAND of lines examples of everything in google. Spend your time reading.
Remove the double quotes in By.XPATH -
browser.find_element(By.XPATH, "//*[#id='gb']/div/div[1]/div/div[1]/a")
and import By:
from selenium.webdriver.common.by import By

execute_script don't work with Youtube Search : Selenium Python

so I'm fairly new to python but I start to understand how things work together, however for this problem I didn't find any solution.
So my problem is I'm making a simple bot with python that can open Youtube and search for specific keywords, Now I have tried the send.keys method and it won't work because I want to use special characters that ChromeWebDriver just can't send due to chromedriver only supports characters in the bmp Problem!
So then I explored my options 1 using GeckoDriver Firefox instead and yeah it worked! however, I couldn't make Firefox undetected as a bot!
option 2 is using the execute_script method with ChromeWebDriver it did work at first with Google search, But then I wanted to use it for Youtube and nothing happens.
I run my script browser works youtube page is loaded but nothing happens the script completes the 20s sleep and exists normally but no input has been made in the search bar or any error in my console just nothing!
maybe the element is not loaded? but I put 20sec sleep after each line of code to make sure the page have enough time to load properly I tried to use Find_by_id | xPath | class_name still nothing
I tried to check if the element is there before executing the input text line and still nothing
Also, I have tried to .click the element before doing text input the search bar is highlighted the click worked but still no text and nothing.
As for the characters I'm trying to input into the search bar are like this: 𝕊 𝕄
which can't be sent by the send.key method
My code:
import undetected_chromedriver.v2 as uc
from time import sleep
import jdk
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
if __name__ == '__main__':
driver = uc.Chrome()
driver.get('https://youtube.com')
sleep(20)
driver.execute_script("document.getElementById('search').checked = true;")
print('Searching for Element')
sleep(20)
Elem_nt = driver.find_element(By.XPATH, "/html/body/ytd-app/div/div/ytd-masthead/div[3]/div[2]/ytd-searchbox/form")
print('Finding Element Now...')
sleep(20)
driver.find_element(By.XPATH, "/html/body/ytd-app/div/div/ytd-masthead/div[3]/div[2]/ytd-searchbox/form").click()
print('Clickling Element Now')
sleep(20)
driver.execute_script("arguments[0].value = 'vid_title';", Elem_nt)
print('input Text Now')
sleep(20)
Any help would be appreciated, also excuse the terms that I use to explain as I'm new to this I just want to explain everything.
Try this one
import pyperclip
from selenium.webdriver.common.keys import Keys
pyperclip.copy(your_text)
element = driver.find_element(By.CLASS_NAME,"your_class_name")
element.send_keys(Keys.CONTROL + "V")

Unable to produce result by using a hashtag in some searchbox

I've written a script in Python using Selenium to log in to Instagram and then search for some hashtag, as in #NewYorkbarbers, and get the link of that hashtag. My script can successfully log in, click on the Not Now button if Turn on Notifications box shows up, and then put that hashtag in the search box, but I can't make my script initiate that search to produce result against that hashtag.
I've tried so far:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
class InstagramScraper:
login_url = 'https://www.instagram.com/accounts/login/?source=auth_switcher'
def __init__(self,username,password):
self.driver = webdriver.Chrome()
self.wait = WebDriverWait(self.driver,10)
self.login(username,password)
def login(self,username,password):
self.driver.get(self.login_url)
self.wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,'input[name="username"]'))).send_keys(username)
self.wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,'input[name="password"]'))).send_keys(password)
self.wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,'button[type="submit"]'))).click()
try:
self.wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,'button.HoLwm'))).click()
except Exception:pass
def use_hashtag(self):
self.wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,'input[placeholder="Search"]'))).send_keys("#NewYorkbarbers",Keys.ENTER)
if __name__ == '__main__':
scraper = InstagramScraper('username','password')
scraper.use_hashtag()
How can I use return within login() method as any ideal method should have return statement within it?
How can I produce the result of that hashtag search?
General Idea of having return statement is to go back.
When you have return in a statement it goes back to the place from where it is got called.
when u write return value it means go back and take this value.
If no return statement is given it means return None
You are using Enter key to initiate the search, but if you go and open Instagram in the browser you will know that even manually that is not possible.
Instagram does not initiate the search simply on a single key hit of enter, when you press enter for the first time it will focus on the very first search suggestion which takes a little bit of time to load (which is not guaranteed to be the #tag that you type so it is better to also check text and the click on particular #tag) and when you press the enter second time it will initiate the search for hashtag that is right now focused.
Even this is not enough, in fact, you need to wait for the suggestion dropdown of the search box to open and then find the desired #tag from the suggested item and press enter.
Basically, the script is too fast to give time for search suggestions to appear (Even manually you can hit Enter two times faster then dropdown to appear and search won't be initiated.) and also it requires two times hit of Enter, first will focus the very first suggestion and second will initiate the search.
In python, not all functions need to return something, and it's not even a convention to have all functions end with a return statement (Unlike other languages such as C). In fact, according to this question, it would be best practice not to have a return statement. But a simple return at the end of the function will be the best option if you really insist on having an explicit return statement.
As for your other problem, a new div appears when the search suggestions have loaded, so you have to wait for that div, then send enter twice.
Here is the new use_hashtag method: (sorry for using xpath and not css selector, but I'm not familiar with css and the chrome developer tools kept giving things dependent on class names):
def use_hashtag(self):
search_input = self.wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,'input[placeholder="Search"]')))
search_input.send_keys("#NewYorkbarbers")
self.wait.until(EC.visibility_of_element_located((By.XPATH, '//*[#id="react-root"]/section/nav/div[2]/div/div/div[2]/div[2]/div[2]')))
search_input.send_keys(Keys.ENTER, Keys.ENTER)

Python Selenium Code behaving differently depending on calling mode

I've tripped on a very easy to reproduce code that's driving me crazy.
I'm trying to take a picture of the screen after entering a container code in a website form, using Python and PhantomJS webdriver (selenium).
The code that makes it possible is quite short, and reproducing it in the console works fine. But if this same code is within a function or a script, it doesn't behave the same.
Here is the working code, that works for me writing it line by line in the console:
(Python version 2.7.9, selenium 2.53.6)
> python
from selenium import webdriver
driver = webdriver.PhantomJS()
driver.set_window_size(1280, 1024)
driver.get('http://www.track-trace.com/container')
driver.find_element_by_name('number').clear()
driver.find_element_by_name('number').send_keys('CGMU5109933')
driver.find_element_by_xpath('//input[#name="commit" and #value="Track direct"]').click()
driver.save_screenshot('./x.png')
However, this same code inside a function or a script only behaves the same until it reaches the click.
It freezes while loading, and the screenshot shows just that.
It doesn't matter what type of wait I try, implicit or explicit, the loading button won't end.
Here is the same code inside a function in foo.py module to make trying it easier.
The picture that is taken won't match the picture taken by the above code.
# foo.py
from selenium import webdriver
def try_it():
driver = webdriver.PhantomJS()
driver.set_window_size(1280, 1024)
driver.get('http://www.track-trace.com/container')
driver.find_element_by_name('number').clear()
driver.find_element_by_name('number').send_keys('CGMU5109933')
driver.find_element_by_xpath('//input[#name="commit" and #value="Track direct"]').click()
driver.save_screenshot('./x.png')
> python
>>> import foo
>>> foo.try_it()
The code must be in a function as it is called on demand when new search petitions arrive on a web service that is integrated in an application.
I always seek first for questions that may have valid answers but this time it doesn't seem to be anything similar to my problem.
Any idea why this may be happening and how to avoid it would be very much appreciated. If any other code or clarification is needed don't hesitate to ask.
This is purely a timing issue. Button clicks don't block, meaning once a click is issued, it returns and python runs the very next line right away. You need to wait until the next page has finished loading before you can take the screenshot. I would use an explicit wait that blocks until an element on the next page you're interested in has loaded. Likewise, I think you do need to worry about the popup asking if you really want to use direct.
My script:
from explicit import waiter
from selenium import webdriver
from selenium.webdriver.common.by import By
def locate_container(driver, container_id):
url = 'http://www.track-trace.com/container'
track_direct_xpath = '//input[#name="commit" and #value="Track direct"]'
im_sure_css = 'div.modal-footer button.jq-directinfo-continue'
tracking_details_header_css = 'div#wrapper > div.inner > h1'
# Load the container search page
driver.get(url)
# Locate the container field, enter container_id, click direct search button
waiter.find_write(driver, 'number', container_id, clear_first=True, by=By.NAME)
waiter.find_element(driver, track_direct_xpath, By.XPATH).click()
# Locate I'm Sure button and click it
waiter.find_element(driver, im_sure_css, By.CSS_SELECTOR).click()
# Wait for the "Tracking details for Container: XXX" header to load
waiter.find_element(driver, tracking_details_header_css, By.CSS_SELECTOR)
# Now we know the page has loaded and we can take the screenshot:
driver.save_screenshot('./x.png')
def main():
driver = webdriver.PhantomJS()
try:
driver.set_window_size(1280, 1024)
locate_container(driver, 'CGMU5109933')
finally:
driver.quit()
if __name__ == "__main__":
main()
(Full disclosure: I maintain the explicit package, which is meant to simplify using explicit waits. You can replace it with direct waits and get the same affect. Simply pip install explicit to install)

Selenium Python open on IE browser

I am using Selenium with python. I am opening a internal link which give me a certificate error which need to click to proceed further.
"Continue to this website (not recommended)."
I used various combinations in my code to overcome this but no help.
Please suggest what code I can modify in python to over come this.
def test_login_aa(self):
driver = self.driver
driver.get("base_url")
time.sleep(5)
#driver.find_elements_by_id.to(('overridelink').click()");
#driver.find_element_by_name("overridelink").click()
#driver.find_element_by_name("overridelink").send_keys(ENTER);
driver.find_element_by_name("overridelink").send_keys(ENTER);
#driver.find_element_by_xpath("//a[#id='overridelink']").click()
#driver.find_elements_by_name("overridelink").sendKeys(Keys.ENTER);
#driver.findElement(By.name("overridelink")).sendKeys(Keys.ENTER);
self.assertEqual("User Login", driver.title)
driver.find_element_by_id("IDToken1").clear()
driver.find_element_by_id("IDToken1").send_keys("username")
I have had luck using this method:
driver.get("javascript:document.getElementById('overridelink').click()")
See also: https://stackoverflow.com/a/11577323/2626096

Categories

Resources