Selenium Web-driver Click Button - python

[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...:)

Related

How to Select Element Based on Parameters Passed in Button Onclick Method

I want to be able to select the button that has the timestamp i am targeting. Here is the HTML:
<a class="mdc-button mdc-button--unelevated mdc-ripple-upgraded" href="#" onclick="selectTime(7160, null, "2021-08-23T08:15:00-04:00", '8b96beed5b0b8f7b34e51853f14ef6eb'); return false;" style="--mdc-ripple-fg-size:55px; --mdc-ripple-fg-scale:1.97805; --mdc-ripple-fg-translate-start:17px, -19.5px; --mdc-ripple-fg-translate-end:18.5px, -9.5px;">
<div class="mdc-button__ripple"></div>
<span class="mdc-button__label">8:15 AM</span>
</a>
I do not know how to target the timestamp parameter it is calling in onclick that can be used to select the button. I have tried to find element by targeting the label's text, but I cannot click on the label, with it giving me this error :
Message: element click intercepted: Element <span class="mdc-button__label">...</span> is not clickable at point (132, 380). Other element would receive the click: <div class="mdc-button__ripple"></div>
Any help would be greatly appreciated.
You can use the below css_selector :
a[class$='mdc-ripple-upgraded'] span[class='mdc-button__label']
use it like this :
wait = WebDriverWait(driver, 10)
ActionChains(driver).move_to_element(wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "a[class$='mdc-ripple-upgraded'] span[class='mdc-button__label']")))).click().perform()
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains

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

Finding li element by text inside - Selenium Python

I have been trying to find a way to click a specific li element by using the text inside, however, whatever I try seems to not be finding the element.
The HTML is;
<ul class="shoes-sizen-mp" id="ul_top_hypers" style="overflow: auto;">
<li id="li_284" onclick="return select_size('284')"><a class="a_top_hypers"> 3.5 <span style="display: none">,</span><br> 35.5<br></a></li>
<li id="li_285" onclick="return select_size('285')"><a class="a_top_hypers"> 4 <span style="display: none">,</span><br> 36<br></a></li>
This is part of a list where you can select your size (such as 3.5,4,4.5,5). I want to be able to click the specific one by using the text such as 3.5, for example.
Edit
driver.find_element_by_xpath(f"//ul[#id='ul_top_hypers' and starts-with(#id, 'li_'][contains(text(),'{user_shoe_size}')]").click()
sleep(20)
Above is one of the many things I have tried to locate the element, but nothing just yet.
Any help would be greatly appreciated. Thanks a lot!
To click on dynamic induce WebDriverWait() and wait for element_to_be_clickable() and following xpath.
def click_size(size):
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//ul[#class='shoes-sizen-mp']//li[contains(.,'{}')]".format(size)))).click()
click_size("3.5")
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

Python Finding element by XPath

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

Button"+" is not being clicked in selenium

Source code:
<input type="button" value="+" id="hour_add" class="ui-button ui-widget ui-state-default ui-corner-all" role="button" aria-disabled="false">
My code:
driver.find_element_by_xpath("//input[contains(#id, 'hour_add')]").click();
This button is not clicked.
There could be multiple reasons for the problem and there is certainly not enough information to answer, but here are the possible reasons:
there are multiple elements matching the XPath expression and a wrong element is clicked
you might need to move to the element and click:
from selenium.webdriver.common.action_chains import ActionChains
ActionChains(driver).move_to_element(element).click(element).perform()
you might need to scroll into view of the element:
driver.execute_script("arguments[0].scrollIntoView();", element)
you might need to click via javascript:
driver.execute_script("arguments[0].click();", element)
you might need to wait for element to be clickable:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
element = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.ID, "hour_add"))
)
element.click()
sometimes even maximizing the browser window can help:
driver.maximize_window()
When you have Id available for the element you want to click on, then just use find_element_by_id.
driver.find_element_by_id('hour_add').click()

Categories

Resources