Selenium i can't click button python - python

I'm try many method but not work U have any solution?
<button type="button" class="btn btn-solid-primary btn--l _3Kiuzg" aria-disabled="false">buy</button>
My code and Error
browser.find_elements_by_css_selector(".btn-solid-primary").click()
AttributeError: 'list' object has no attribute 'click'
i can try
find_elements_by_class_name
not work
browser.find_element_by_class_name("btn btn-solid-primary btn--l _3Kiuzg")
error
Traceback (most recent call last):
File "D:\Project-Program\selenium\test3.py", line 11, in <module>
browser.find_element_by_class_name("btn btn-solid-primary btn--l _3Kiuzg")
File "D:\Project-Program\selenium\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 564, in find_element_by_class_name
return self.find_element(by=By.CLASS_NAME, value=name)
File "D:\Project-Program\selenium\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "D:\Project-Program\selenium\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "D:\Project-Program\selenium\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".btn btn-solid-primary btn--l _3Kiuzg"}

Here is the ans - Change elements to element in the below code.
browser.find_element_by_css_selector(".btn-solid-primary").click()

Also, I assume, you're parsing Instagram. It uses dynamic classes (In your case, _3Kiuzg), that change after page reload. So ensure, you're not looking for element by it. Function find_element_by_class_name is not taking many classes, so, if you want such, you should use find_element_by_css_selector, and selector .class_1.class_2.etc

Related

Click on DIV with selenium [duplicate]

This question already has an answer here:
"unknown error: cannot read property 'scrollleft' of null" in Chrome using selenium
(1 answer)
Closed 3 years ago.
I'm trying to click on "retweet" with selenium.
I found this way with javascript:
document.querySelector('[data-testid="retweet"]').click()
document.querySelector('[data-testid="retweetConfirm"]').click()
So I implemented in selenium with something like this:
firstJS = "document.querySelector('[data-testid=\"retweet\"]').click()"
secondJS ="document.querySelector('[data-testid=\"retweetConfirm\"]').click()"
time.sleep(5)
driver.execute_script(firstJS)
time.sleep(3)
driver.execute_script(secondJS)
and i get this error:
Traceback (most recent call last):
File "C:\Users\Riccardo\Desktop\ProxyGiveaway\config.py", line 75, in <module>
main()
File "C:\Users\Riccardo\Desktop\ProxyGiveaway\config.py", line 67, in main
retweet(link, driver)
File "C:\Users\Riccardo\Desktop\ProxyGiveaway\config.py", line 43, in retweet
driver.execute_script(firstJS)
File "C:\Users\Riccardo\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 636, in execute_script
'args': converted_args})['value']
File "C:\Users\Riccardo\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\Riccardo\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.JavascriptException: Message: javascript error: Cannot read property 'click' of null
(Session info: chrome=1.2.3.4)
EDIT: I added some time.sleep() and the first line works, but not the second
This might be due to page loading try adding wait until page load.

Why is my Selenium xpath expression returning an [object attribute] instead of an element?

I'm making a scraper that will through my webpage and grab all of the links. A lot of the links are in closed list also known as a tree. Therefore, I found the xpath that holds all of the links. I ran the following xpath in google inspect and it ran perfectly fine giving me the following output.
var result=$x("//div[#id='index__tree']//a[contains(text(),doku.php)]/#href")
result[0].value
"/doku.php?ihome"
result[4].value
"/doku.php?start"
I than transferred the xpath into selenium code:
a = driver.find_elements_by_xpath("//div[#id='index__tree']//a[contains(text(),doku.php)]/#href")
for aa in a:
print(aa)
I then ran the code and received the following error:
opening browser
Login Successful
Traceback (most recent call last):
File "wiki.py", line 49, in <module>
a = driver.find_elements_by_xpath("//div[#id='index__tree']//a[contains(text(),doku.php)]/#href")
File "/home/aevans/wikiProject/venv/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 410, in find_elements_by_xpath
return self.find_elements(by=By.XPATH, value=xpath)
File "/home/aevans/wikiProject/venv/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 1007, in find_elements
'value': value})['value'] or []
File "/home/aevans/wikiProject/venv/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/home/aevans/wikiProject/venv/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: The result of the xpath expression "//div[#id='index__tree']//a[contains(text(),doku.php)]/#href" is: [object Attr]. It should be an element.
(Session info: headless chrome=73.0.3683.86)
(Driver info: chromedriver=73.0.3683.86,platform=Linux 3.10.0-957.12.2.el7.x86_64 x86_64)
Try replacing
a = driver.find_elements_by_xpath("//div[#id='index__tree']//a[contains(text(),doku.php)]/#href")
for aa in a:
print(aa)
with
a = [elem.get_attribute("href") for elem in driver.find_elements_by_xpath("//div[#id='index__tree']//a[contains(text(),doku.php)]")]
for aa in a:
print(aa)
Notice I removed the "/#href" from the end of your selector.
The Selenium selectors must return a WebElement. By specifying the "/#href", it returned the href attribute of that element instead of the element itself.
The method get_attribute(attribute_name) returns the attribute of an element. Then, you can loop through it.

How to find elements of a modal pop up window with Selenium Webdriver

I am using Selenium webdriver with firefox to download real states data from a website. Once the inputs are entered a pop up window will ask to either select 'Cancel' or 'Save File'.
The problem is that I'm not able to find how to 'click' these buttons. I know it is already the current window in my driver because I am able to change the enter image description here position.
If I dodriver.find_element_by_xpath('//*'), this is the error I get:
Traceback (most recent call last):
File "<ipython-input-131-e87e382e3fb9>", line 1, in <module>
driver.find_element_by_xpath('//*')
File
"/home/rcortez/anaconda2/envs/webscraper/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py",
line 295, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File
"/home/rcortez/anaconda2/envs/webscraper/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py",
line 756, in find_element
'value': value})['value']
File
"/home/rcortez/anaconda2/envs/webscraper/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py",
line 238, in execute
self.error_handler.check_response(response)
File
"/home/rcortez/anaconda2/envs/webscraper/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py",
line 193, in check_response
raise exception_class(message, screen, stacktrace)
NoSuchWindowException: No such content frame; perhaps the listener was not registered
I've searched on the web for solutions and most of them mention switching to alert, driver.switch_to_alert().accept(). This is the error I'm getting:
Traceback (most recent call last):
File "<ipython-input-142-bd939a2be33d>", line 1, in <module>
driver.switch_to_alert().accept()
File
"/home/rcortez/anaconda2/envs/webscraper/lib/python2.7/site-packages/selenium/webdriver/common/alert.py",
line 81, in accept
self.driver.execute(Command.ACCEPT_ALERT)
File
"/home/rcortez/anaconda2/envs/webscraper/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py",
line 238, in execute
self.error_handler.check_response(response)
File
"/home/rcortez/anaconda2/envs/webscraper/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py",
line 193, in check_response
raise exception_class(message, screen, stacktrace)
NoAlertPresentException: No tab modal was open when attempting to get the dialog text
This issue is very similar to Not able to locate element on a modal pop up window : selenium, but I don't understand how the accepted answer there was able to find the selectors.
Any ideas how to solve this issue would be greatly appreciated.

selenium webdriver locate the element

i try to use selenium webdriver to locate a element:
i want click the "用户查询"
corresponding html:
the red frame
python:
>>> driver.find_element_by_xpath("//div[#id='container']/h1[2]/a").click()
Traceback (most recent call last):
File "<pyshell#22>", line 1, in <module>
driver.find_element_by_xpath("//div[#id='container']/h1[2]/a").click()
File "D:\Python35\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 293, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "D:\Python35\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 752, in find_element
'value': value})['value']
File "D:\Python35\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "D:\Python35\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 192, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element
(Session info: chrome=52.0.2743.116)
(Driver info: chromedriver=2.7.236900,platform=Windows NT 6.1 SP1 x86)
how to locate this element?
Try this below xpath with indexing.
Here. [1] indicates index number of your a tag. suppose in your html, there are numbers of a tag available with different names like this. 用户查询. Suppose, This text 用户查询 index number is one inside your html then use below xpath.
//h1[1]/a
If This text 用户查询 index number is two inside your html then use below xpath.
//h1[2]/a
You could try locating the element based on it's link text:
driver.find_element_by_link_text("用户查询").click();

CLOSED - Python Selenium Can't find elements

I have a problem with selenium with python 3.5, after install all the windows updates all my selenium script broken, I receive every time the same error:
Traceback (most recent call last):
File "C:/Users/Carlo/Desktop/CEx/src/IE.py", line 12, in
a=driver.find_element_by_xpath("//*[#id='un']")
File "C:\Python35-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 293, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "C:\Python35-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 752, in find_element
'value': value})['value']
File "C:\Python35-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "C:\Python35-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 192, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchWindowException: Message: Unable to find element on closed window
But I really don't understand why because the window of IE is open!
This is my code (simple just to check why I can't make it work more):
import time from
selenium import webdriver
driver = webdriver.Ie()
driver.get('http://gala.test-platform.celtrino.com/Login.aspx')
time.sleep(10)
driver.find_element_by_xpath(".//*[#id='un']")
The code fail every time in the last line and I double check with firepath and the xpath is correct so I don't really understand why it's not working.
I knew, IE has problems with xpath. It does not support xpath directly. It needs third party tools to do this. So, I suggest you to try cssSelector or any other options instead. Since, the element has an ID so you could use this. It's better.
driver.find_element_by_id("un");

Categories