Is it possible to click button class in Selenium Python? - python

when I use browser.find_element_by_css_selector(".LkLjZd ScJHi OzU4dc ").click(),
Message: no such element: Unable to locate element: {"method":"css selector","selector":".LkLjZd ScJHi OzU4dc "}
This error occurs despite of element is that <button class="LkLjZd ScJHi OzU4dc " jsaction="click:TiglPc" jsname="gxjVle" style="font-family: Roboto, "Noto Sans KR", sans-serif;">total review</button>
how to click the butten....

browser.find_element_by_xpath("//button[.='total review']").click()
You can click on it by it's text value most of it's identifiers seems dynamic

you can use the xpath of the element.
Install xpath Helper in your chrome.
Refresh the page or close it and enter again.
Click on addon from the toolbar.
Shift + click the element you needed.
Than browser.find_element_by_xpath()
PS: If it's a long string you can cut the start of it and replace with another /.

Related

ElementNotInteractableException: Message: Element <button class="btn btn-outline-light btn-icons" type="button"> could not be scrolled into view

I am trying to press a the download on this page
https://data.unwomen.org/data-portal/sdg?annex=All&finic[]=SUP_1_1_IPL_P&flocat[]=478&flocat[]=174&flocat[]=818&flocat[]=504&flocat[]=729&flocat[]=788&flocat[]=368&flocat[]=400&flocat[]=275&flocat[]=760&fys[]=2015&fyr[]=2030&fca[ALLAGE]=ALLAGE&fca[<15Y]=<15Y&fca[15%2B]=15%2B&fca[15-24]=15-24&fca[25-34]=25-34&fca[35-54]=35-54&fca[55%2B]=55%2B&tab=table
i am using python selenium with firefox and this is what i tried:
driver.set_page_load_timeout(30)
driver.get(url)
time.sleep(1)
WebDriverWait(driver, timeout=20).until(EC.presence_of_element_located((By.ID, 'SDG-Indicator-Dashboard')))
time.sleep(1)
download_div = driver.find_element(By.CLASS_NAME, 'float-buttons-wrap')
buttons = download_div.find_elements(By.TAG_NAME, 'button')
buttons_attributes = [i.get_attribute('title') for i in buttons]
download_button_index = buttons_attributes.index('Download')
buttons[download_button_index].location_once_scrolled_into_view
buttons[download_button_index].click()```
i keep getting the same error:
ElementNotInteractableException: Message: Element <button class="btn btn-outline-light btn-icons" type="button"> could not be scrolled into view
eventho i am getting the correct element and i tried using js like this:
```driver.execute_script("return arguments[0].scrollIntoView(true);", element)```
also did not work.
You have to modify the XPath, try the below code:
driver.get("https://data.unwomen.org/data-portal/sdg?annex=All&finic[]=SUP_1_1_IPL_P&flocat[]=478&flocat[]=174&flocat[]=818&flocat[]=504&flocat[]=729&flocat[]=788&flocat[]=368&flocat[]=400&flocat[]=275&flocat[]=760&fys[]=2015&fyr[]=2030&fca[ALLAGE]=ALLAGE&fca[<15Y]=<15Y&fca[15%2B]=15%2B&fca[15-24]=15-24&fca[25-34]=25-34&fca[35-54]=35-54&fca[55%2B]=55%2B&tab=table")
driver.implicitly_wait(15)
time.sleep(2)
download_btn = driver.find_element(By.XPATH, "(.//button[#type='button' and #title='Download'])[2]")
download_btn.location_once_scrolled_into_view
time.sleep(1)
download_btn.click()
1.You need to make sure you are giving enough time to complete page loading
2.Once you started to find button element , it's better to use try/catch blocks multiple times and put sleep function when exception occures to provide appropriate time to load scripts and elements.
3.Try finding by xpath instead of finding elements by indexes and be aware that you need to use the index [1] instead of [0] in a xpath string

Selenium Python Checkbox Click

I am trying to access the following HTML checkbox for a button click:
<input type="checkbox" data-ng-value="sf.name" data-ng-model="sf.checked" ng-click="ec.onStateFilterChanged(sf)" title="Select a state" class="ng-untouched ng-valid ng-dirty ng-valid-parse" value="Arizona">
using:
state = driver.find_element_by_xpath("input[#type='checkbox']").click()
but keep getting error:
selenium.common.exceptions.NoSuchElementException: Message:
what might be the element path I am looking for in order to select the checkbox?
Your xpath is most likely incorrect - you need to enter // before the element as this will find all (single slash / will work here too though as you are only trying to find one element and it will find the first match)
Try one of the following:
state = driver.find_element_by_xpath("//input[#type='checkbox']").click()
OR
state = driver.find_element_by_css_selector("input[type='checkbox']").click()

NoSuchElementException when using Selenium Python [duplicate]

This question already has an answer here:
Selenium "selenium.common.exceptions.NoSuchElementException" when using Chrome
(1 answer)
Closed 2 years ago.
I'm trying to scrape the promotion information of each product from a website by clicking on the product and go to its detailed page. When the spider clicks on the product, the web will ask it to log in, and I tried the following code:
def __init__(self):
self.driver = webdriver.Chrome(executable_path = '/usr/bin/chromedriver')
...
def start_scraping(self, response):
self.driver.get(response.url)
self.driver.find_element_by_id('fm-login-id').send_keys('iamgooglepenn')
self.driver.find_element_by_id('fm-login-password').send_keys('HelloWorld1_')
self.driver.find_element_by_class_name('fm-button fm-submit password-login').click()
...
However, there is NoSuchElementException when I run it.
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="fm-login-id"]"}
'spider_exceptions/NoSuchElementException': 14,
The HTML of the login page is as follows:
<div class='input-plain-wrap input-wrap-loginid'>
<input id='fm-login-id' class='fm-text' name='fm-login-id'...>
event
</div>
So, I'm pretty sure the id should be 'fm-login-id'. The reason I could think of that might cause this issue is that this login page is a popup.
Basically, it pops up in the middle of the main page. Looking at the HTML of the site, I can see that the login type seems to be a new HTML window
<!DOCTYPE html>
<html>event
....
<\html>
I'm not sure if this is the issue, and if so, how to fix it? Also, is there other reasons that might've caused the issue?
The popup will have an ID. You might have to add f'#{popup_id}' to the end of response.url. Like this URL: https://stackoverflow.com/questions/62906380/nosuchelementexception-when-using-selenium-python/62906409#62906409. It contains #62906409 because 62906409 is the ID of an element in the page.
The login page inside a frame, you need switch it first:
#switch it first
self.driver.switch_to.frame(driver.find_element_by_id('J_loginIframe'))
self.driver.find_element_by_id('fm-login-id').send_keys('iamgooglepenn')
self.driver.find_element_by_id('fm-login-password').send_keys('HelloWorld1_')
And for login button you can't use .find_element_by_class_name, this method just for single class name. This element having multiple class name, so use .find_element_by_css_selector like bellow:
#submit button
self.driver.find_element_by_css_selector('.fm-button.fm-submit.password-login').click()
The login content seems to be nested in an iFrame element (if you trace it all the way to the top, you should find an iFrame with id="sufei-dialog-content"), which means you need to switch to that iFrame for that nested html before selecting your desired element, otherwise it will not work.
First you will need to use driver.switch_to.frame("sufei-dialog-content"), and then select your element with driver.find_element_by_name() or whatever you had.
A similar issue can be found here: Selenium and iframe in html
Just a simple mistake:
<div class='input-plain-wrap input-wrap-loginid'>
<input id='fm-login-id class='fm-text' name='fm-login-id'...>
event
</div>
is actually supposed to be:
<div class='input-plain-wrap input-wrap-loginid'>
<input id='fm-login-id' class='fm-text' name='fm-login-id'...>
event
</div>
You forgot a single-quote.
Have you tried driver.find_element_by_name('fm-login-id')?
You should try finding the elements by their XPaths. You just have to inspect the element, right-click on it and copy its XPath. The XPath of the first <input ... is //*[#id="fm-login-id"].

Xpath exists in chrome, but not in my script. no such element: Unable to locate element:

I am trying to find the button element by xpath, which is found when I type it in chrome, but the script gives me the no attribute id error. I have tried switching into an iframe and frame, I have used webdriver.wait to wait for the button element to show and none of those work. I would also like to cycle through and click the first button if it says "Follow" and then move to the next button if it says "Follow".The Script runs on chrome and I am trying to do this on instagram Html here
popup = browser.find_element_by_xpath('//button[#class="_qv64e _gexxb _4tgw8 _njrw0"]')
ActionChains(browser)\
.move_to_element(popup).click()\
.perform()
File "/Users/trevaroneill/PycharmProjects/Insta/instafollow.py", line 91, in <module>
popup = browser.find_element_by_xpath('//button[#class="_qv64e _gexxb _4tgw8 _njrw0"]')
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//button[#class="_qv64e _gexxb _4tgw8 _njrw0"]"}
Since you are using find_elements, you should rather write:
ActionChains(browser)\
.move_to_element(popup[0]).click()\
.perform()
in order to access first element of the list returned by find_elements.
The problem is if you have several webelements selected by your xpath, in this case, it is not sure that the first one is the one you actually aim at. I would suggest you use find_element if there are no particular reason for which you are using find_elements
You are getting the wanted element in a list.
change
popup = browser.find_elements_by_xpath('//button[#class="_qv64e _gexxb _4tgw8 _njrw0"]')
into
popup = browser.find_element_by_xpath('//button[#class="_qv64e _gexxb _4tgw8 _njrw0"]')
find_element and not find_elements.

Python & Selenium - unknown error: Element is not clickable at point (663, 469). Other element would receive the click:

I have this Selenium Code that should click on a size selection button.
submit_button = driver.find_element_by_class_name('pro_sku')
elementList = submit_button.find_elements_by_tag_name("a")
elementList[3].click()
It works for other pages but now on one page I get this error:
selenium.common.exceptions.WebDriverException: Message: unknown error: Element is not clickable at point (663, 469). Other element would receive the click:
I don't understand it because I can look at the browser window that Selenium opens and I normally can click on these buttons.
How can I solve this?
Someone asked for the website. Here it is: http://de.sinobiological.com/GM-CSF-CSF2-Protein-g-19491.html
You can use Xpath for element selection and then use the following method
# Click on Element
def element_click(self, xpath):
xpath = re.sub('"', "'", xpath)
browser.execute_script("""
var elements = document.evaluate("%s",
document,
null,
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
null);
var im = elements.snapshotItem(0);
im.click();
""" %(xpath)
)
So if your x-path is correct and item is present on DOM then definitely it will get clicked.
Happy Coding
You can use action_chains to simulate mouse movment
actions = ActionChains(driver)
actions.move_to_element(elementList[3]).perform()
elementList[3].click()
Edit
The <a> tags are not the actual sizes. Try
sizes = driver.find_elements_by_class_name('size_defaut')
sizes[3].click()
Try below:-
driver.execute_script("arguments[0].click();", elementList[3])
Hope it will help you :)

Categories