Python Finding element by XPath - python

For reference visit https://rabirius.me/2020/02/14/bird-watching/ (not my website)
You may see a Like button there near a reblog button
I want python to click that but I get an error stating
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div/div/div[2]/a"}
The code i wrote was
for posts in open_links:
bot.get(posts)
sleep(4)
bot.find_element_by_xpath('/html/body/div/div/div[2]/a').click()
sleep(2)
The HTML of the like button is
<div class="wpl-button like">
<a href="#" title="177 bloggers like this." class="like sd-button" rel="nofollow">
<span>Like</span>
</a>
</div>
Any Help would be Appreciated

An iframe is present on the page, so you need to first switch to that iframe and then operate on the element and its recommended to use explicit wait to wait for the element to be present on the page.
You can do it like:
for posts in open_links:
bot.get(posts)
driver.switch_to.frame(bot.find_element_by_tag_name('iframe'))
like_element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[contains(#class,'wpl-likebox')]//span[text()='Like']")))
like_element.click()
You need to add the following imports:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Related

Selenium Python - Nothing happens after click on element

I am new to selenium and I try to navigate on a page and to click on a button to get to the next page with selenium web driver.
This is my python code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH, '//*[#id="page"]/div/div/div[5]/div/div/div/div[3]/div/span[2]')))
driver.find_element(By.XPATH, '//*[#id="page"]/div/div/div[5]/div/div/div/div[3]/div/span[2]').send_keys(Keys.RETURN)
print('Element found')
In addition to send_keys(Keys.RETURN) I also tried send_keys(Keys.ENTER) and click(). In all cases the statement "Element found" is printed to the console, but nothing happens on the webpage.
Here is the HTML bit of the button:
<span class="ACME-src-ACME-ui-Pagination--arrowRight">
<i class="flaticon-right-arrow"></I>
</span>
Every help is highly appreciated.
Wait!! Looks like The Xpath you are using is incorrect. Spans are not clickable objects. Or like they are clickable but do not do anything, which is exactly happening here. May be the "i" tag is something which should take click(However I cannot see any object in Dom taking click and performing a job)
First provide me with the proper DOM.
You can further try two more ways of doing it:-
button = find_element(By.XPATH, '//*[#id="page"]/div/div/div[5]/div/div/div/div[3]/div/span[2]/i')
driver.execute_script("arguments[0].click();", button)
OR
button = find_element(By.XPATH, '//*[#id="page"]/div/div/div[5]/div/div/div/div[3]/div/span[2]/i')
ActionChains(driver).move_to_element(button).click().perform()

I keep getting a NoSuchElementException in selenium even though the element DOES exist

This is my first scraping project using selenium. I'm trying to download a couple of reddit videos saved in a list using this website.
As you can see, it shows an input tag where I need to enter the url of the video or gif then go to the download page. That input tag has a class name form-control form-control-lg form-control-alternative. So when I try getting that element so I can fill it with a link from a list in Python, it shows a selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: .form-control form-control-lg form-control-alternative error.
You can check yourselves using the Developer Tools and you'll see that that input tag has that class.
Here's my code:
for gif in gif_list:
driver.get('https://keepv.id/reddit-video-downloader')
input_tag = driver.find_element_by_class_name('form-control form-control-lg form-control-alternative')
input_tag.send_keys(gif)
go_button = driver.find_element_by_class_name('btn btn-danger')
go_button.click()
second_button = driver.find_element_by_class_name('btn btn-danger sheen waggle spin')
second_button.click()
WebDriverWait(driver, 5).until(expected_conditions.presence_of_element_located((By.CLASS_NAME, 'row')))
download_button = driver.find_element_by_class_name('btn btn-lg btn-danger mb-3 shadow vdlbtn')
download_button.click()
gif_url = driver.current_url()
download_gif(gif_url)
find_element_by_class_name() accept single class only use css selector instead.
driver.find_element_by_css_selector(".form-control.form-control-lg.form-control-alternative").send_keys(gif)
driver.find_element_by_css_selector(".btn.btn-danger").click()
Or you can use by_id
driver.find_element_by_id("dlURL").send_keys(gif)
driver.find_element_by_id("dlBTN1").click()
Ideally you should use WebDriverWait() and wait for element_to_be_clickable() and following css selecor
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,".form-control.form-control-lg.form-control-alternative"))).send_keys(gif)
To click go button
WebDriverWait(browser,20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,".btn.btn-danger"))).click()
you need to import below libraries
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

Need help using selenium

I want to go to this site:
http://www.datiopen.it/it/opendata/Mappa_delle_stazioni_ferroviarie_in_Italia
Then click on the "Tabella" tab
Then see the second page (out of 64) of the table
However I failed in the first part, I cannot make a code to click on "Tabella" tab
import requests
from selenium import webdriver
driver.get('http://www.datiopen.it/it/opendata/Mappa_delle_stazioni_ferroviarie_in_Italia')
element = driver.find_element_by_id("Tabella")
element.click()
And here is the html code that I used to search:
<li id="Tabella" class="Table_img ui-state-default ui-corner-top ui-tabs-selected ui-state-active">
<span id="span_table_img" class="span_img"></span>
Tabella </li>
Thank all!
Add time sleep before accessing the table element.
time.sleep(5)
driver.find_element_by_id('Tabella').click()
To click on Tabella tab you need to induce WebDriverWait() and wait for element_to_be_clickable() and you can use following locator.
Link Text:
driver.get("http://www.datiopen.it/it/opendata/Mappa_delle_stazioni_ferroviarie_in_Italia")
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.LINK_TEXT,"Tabella"))).click()
XPATH:
driver.get("http://www.datiopen.it/it/opendata/Mappa_delle_stazioni_ferroviarie_in_Italia")
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//a[text()='Tabella']"))).click()
You need to import below libraries.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Problem with your code
You are trying to click an element before it is available/ready to click in DOM.
Solution
You need to wait for the element to load and become clickable to perform any kind of action on the same element.
Code
driver = webdriver.Chrome()
driver.get('http://www.datiopen.it/it/opendata/Mappa_delle_stazioni_ferroviarie_in_Italia')
def wait_for_element_to_be_clickable(element):
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, element)))
wait_for_element_to_be_clickable("Tabella")
element = driver.find_element_by_id("Tabella")
element.click()
print("executed")
def wait_for_element_to_be_clickable(element): this method will wait for any element to become clickable before proceeding. (you can increase the time based on your page loading time).

Python - Selenium Unable to locate element on Search button

I'm trying to locate and click on a button "Search" on internal website using python selenium.
The element with class = "button":
<a href="javascript:showSearch(true)" title="Search" class="button" id="button_search">
<img src="images/icon_search.gif" alt="">Search
</a>
When click, this button will show a table containing many search filters, and the class changes to "button_active"
<a href="javascript:showSearch(true)" title="Search" class="button_active" id="button_search">
<img src="images/icon_search.gif" alt="">Search
</a>
And xpath is:
//*[#id="button_search"]
locate the css
Packages:
import selenium
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
I've tried all the following code:
search_button = driver.find_element_by_id('button_search')
or
search_button = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//*[#id="button_search"]')))
or
search_button = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//*[#title="Search" and #class="button" and #id="button_search"]')))
and many way by ID, LINK_TEXT.... but I always get the same error that:
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="button_search"]"}
(Session info: chrome=79.0.3945.79)
Do you have any ideas why it may not be able to locate this button?
Thank you!
Edit:
I've tried switch_to.frame and it works!
Thank you all!

Selenium Web-driver Click Button

[Python 2.7, Selenium Web-driver]
So there's this Website:
<div class="flex-module-header">
<h3><span class="trend-location js-trend-location">Greece Trends</span></h3>
<span class="middot">ยท</span> <a role="button" href="#" data-modal="change-trends" class="change-trends js-trend-toggle">Change</a>
And I want to click the button:
<a role="button" href="#" data-modal="change-trends" class="change-trends js-trend-toggle">Change</a>
And I've tried everything (trying to locate by xpath, class etc.), and I can't seem to be able to do what I'm looking for.
I basically want to click the button, that's all.
As you didn't provide error stack trace and tried code, Assuming you didn't use WebDriverWait yet to wait until element visible and clickable. So I'm giving the example with this as below :-
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
link = wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "Change")))
link.click()
Note :- Before using this, make sure element is not present inside a frame or iframe. If element is present inside a frame or iframe you need to switch that frame before finding the button as driver.switch_to_frame("frame name or id").
Hope it helps...:)

Categories

Resources