Click() function not working in python / selenium - python

as you can see I'm starting coding a bot to cop sneakers on SNKRS. I've already coded a few useless things but we don't care. I want the bot to click on the log in button, but at the end when I run the code, it opens Chrome then it opens the nike website but then it doesn't click on the button and I have this error message:
Traceback (most recent call last):
File "C:/Users/xxx/xxx/xxx/xxx/xxx/SNKRS_bot/snkrs bot.py", line 11, in <module>
loginBtn = driver.find_elements_by_xpath("/html/body/div[2]/div/div/div[1]/div/header/div[1]/section/div/ul/li[1]/button").click()
AttributeError: 'list' object has no attribute 'click'
Here is my code :
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
import time
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://www.nike.com/ch/fr/launch?s=upcoming")
time.sleep(3)
loginBtn = driver.find_elements_by_xpath("/html/body/div[2]/div/div/div[1]/div/header/div[1]/section/div/ul/li[1]/button").click()
time.sleep(6)
driver.quit()
Thanks a lot

See my comment to your posted question. I did in the end find your button, but it is not clickable without resorting to using JavaScript:
from selenium import webdriver
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
try:
driver.implicitly_wait(6) # wait up to 6 seconds to find an element
driver.get("https://www.nike.com/ch/fr/launch?s=upcoming")
loginBtns = driver.find_elements_by_xpath("/html/body/div[2]/div/div/div[1]/div/header/div[1]/section/div/ul/li[1]/button")
if loginBtns:
#loginBtns[0].click()
driver.execute_script('arguments[0].click()', loginBtns[0])
else:
print('Could not find login button.')
finally:
driver.quit()

Related

Unable to pull text from elements within shadow-root using Python Selenium

I am having a terrible time getting text from shadow-root. I found several docs and it looks right to me, except i always get:
Traceback (most recent call last):
File "C:\python\vttest.py", line 29, in
print(driver.execute_script("return document.querySelector('vt-ui-shell').shadowRoot.querySelector('url-view').shadowRoot.querySelector('vt-ui-main-generic-report').shadowRoot.querySelector('vt-ui-url-card').shadowRoot.querySelector('vt-ui-generic-card').shadowRoot.querySelector('p')").text)
File "C:\Users\barberion.NATJ\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 634, in execute_script
return self.execute(command, {
File "C:\Users\barberion.NATJ\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\barberion.NATJ\AppData\Local\Programs\Python\Python38\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 properties of null (reading 'shadowRoot')
(Session info: chrome=96.0.4664.93)
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import DesiredCapabilities # necessary in headless mode, need this library to accept ssl
from selenium.webdriver import Chrome
from selenium.webdriver.support.ui import WebDriverWait
#from selenium.webdriver.support.select import Select
capabilities = DesiredCapabilities.CHROME.copy()
capabilities['acceptSslCerts'] = True
capabilities['acceptInsecureCerts'] = True
opts = Options()
#opts.headless = True # comment this out to get out of headless
#opts.add_argument("--window-size=1400x1400")
opts.add_argument("--enable-javascript")
opts.add_argument("--start-maximized")
opts.add_argument('--disable-gpu')
searchvt=input("Enter URL: ")
driver = Chrome(options=opts,desired_capabilities=capabilities,executable_path='C:/python/chromedriver.exe')
driver.get('https://www.virustotal.com/gui/home/url')
url='//*[#id="view-container"]/home-view'
time.sleep(.5)
driver.find_element_by_xpath(url).send_keys(searchvt)
time.sleep(.5)
driver.find_element_by_xpath(url).send_keys(Keys.RETURN)
driver.maximize_window()
driver.set_page_load_timeout(5)
time.sleep(3)
print(driver.execute_script("return document.querySelector('vt-ui-shell').shadowRoot.querySelector('url-view').shadowRoot.querySelector('vt-ui-main-generic-report').shadowRoot.querySelector('vt-ui-url-card').shadowRoot.querySelector('vt-ui-generic-card').shadowRoot.querySelector('p')").text)
my error must be somewhere in the print :( any help is most appreciated!
Edit:
To be more specific, I want to submit an URL or domain to virustotal, and then read the result text on top of page. If you use MSN.com the text I want is on top where it says "No security vendors flagged this URL as malicious"
If you update to Selenium 4.0 and use a Chromium browser v96+ you can use the new shadow_root property in Python Selenium and avoid using JavaScript entirely.
This is a working example:
driver.get('http://watir.com/examples/shadow_dom.html')
shadow_host = driver.find_element(By.CSS_SELECTOR, '#shadow_host')
shadow_root = shadow_host.shadow_root
shadow_content = shadow_root.find_element(By.CSS_SELECTOR, '#shadow_content')
assert shadow_content.text == 'some text'
Source: https://titusfortner.com/2021/11/22/shadow-dom-selenium.html
My shadow DOMS were just out of order. once i modified print to:
print(driver.execute_script("return document.querySelector('url-view').shadowRoot.querySelector('vt-ui-url-card').shadowRoot.querySelector('vt-ui-generic-card').querySelector('p')").text)
everything functioned as expected.

I am running into this error with Python Selenium with the timing of the webpage load

# Path to the chromedriver program
service = Service('C:\Program Files (x86)\Google\chromedriver.exe')
service.start()
# Driver opens the remote with robinhood website
driver = webdriver.Remote(service.service_url)
driver.get('https://robinhood.com/crypto/BTC')
# We will grab the element id's to log on to Robinhood
# driver.find_element_by_id(“ID”).send_keys(“username”)
# driver.find_element_by_id (“ID”).send_keys(“password”)
# driver.find_element_by_id(“submit”).click()
signinButton = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, "_3kh8OsNx6QdAbMaoKTi2Yq _1uaripz9PIQ8yApSTs6BKk")))
# driver.find_element_by_class_name('_3kh8OsNx6QdAbMaoKTi2Yq _1uaripz9PIQ8yApSTs6BKk')
signinButton.click()
# Closes the driver after timeout
driver.quit()
I am basically opening up chrome webdriver and going to the robinhood website, however I am running into a webpage load issue. In order to fix it I was attempting to use WebDriverWait to halt the button click until the webpage loads.
The problem is the button click does not execute once 10 secs pass and instead throws this error:
Traceback (most recent call last):
File "D:/gitRepos/bitmine/runmine.py", line 25, in <module>
signinButton = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, "_3kh8OsNx6QdAbMaoKTi2Yq _1uaripz9PIQ8yApSTs6BKk")))
File "D:\Programs Files 2\Python\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
This error occurs due to syncghronization issue. Yu can resolved your issue by using waits in selenium. Please refer below solution to avoid such errors:
WebDriverWait(driver, 30).until(
EC.element_to_be_clickable((By.XPATH, "//button[#class='_3kh8OsNx6QdAbMaoKTi2Yq _1uaripz9PIQ8yApSTs6BKk']"))).click()
Note : please add below imports to your solution
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
Section for Sign up for free button:
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[#class='_3kh8OsNx6QdAbMaoKTi2Yq _1uaripz9PIQ8yApSTs6BKk']"))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, "//span[contains(text(),'Sign up for free')]"))).click()
That error means it did not find a clickable button within the 10 seconds, and it Timed Out, throwing a TimeoutException. You need to set a longer wait time, or handle the TimeoutException accordingly
The error usually spit out due to not able to find the object within defined time period. I rather you set up an exception error to catch it and proceed to find next object or element if it fails.
try:
#Insert your scraping action here
signinButton.click()
except NoSuchElementException:
The error means that selenium were unable to locate the element within the time specified.
Also don't use spaces within class name. just use dot . otherwise whatever you increased the time, selenium will not be able to find it .
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("https://robinhood.com/crypto/BTC")
element = driver.find_element_by_class_name(
"_3kh8OsNx6QdAbMaoKTi2Yq._1uaripz9PIQ8yApSTs6BKk")
print(element)

Instagram login script with selenium, not being able to execute .send_keys('test')

Ok so, as the title says, pretty much i just cant make my python program work, it is a bot to log in to instagram and it wont actually do the logging in part, i've tried both using action chains, and without them, this is my code (python 3.6.1) :
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
browser = webdriver.Chrome()
browser.get('https://www.instragram.com')
login_elem = browser.find_element_by_xpath('//*[#id="react-root"]/section/main/article/div[2]/div[2]/p/a')
login_elem.click()
user = browser.find_element_by_name("username")
passw = browser.find_element_by_name('password')
ActionChains(browser)\
.move_to_element(user).click()\
.send_keys('test')\
.move_to_element(passw).click()\
.send_keys('test')\
.perform()
login_button = browser.find_element_by_class_name(
'_0mzm- sqdOP L3NKy ')
login_button.click()
Pretty simple, right? and at since there are no syntax errors, i truly am confused over what is going wrong, since once I run my code, nothing happens after going to the login page. (Which means, it's not actually actuating on the username and password forms)
This is what the Module says :
Traceback (most recent call last): File
"C:\Users\ferna\Desktop\trying browser script.py", line 17, in
.send_keys('test')\ File "C:\Users\ferna\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\common\action_chains.py",
line 83, in perform
action() File "C:\Users\ferna\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\common\action_chains.py",
line 277, in
Command.MOVE_TO, {'element': to_element.id})) File "C:\Users\ferna\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py",
line 321, in execute
self.error_handler.check_response(response) File "C:\Users\ferna\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py",
line 242, in check_response
raise exception_class(message, screen, stacktrace) selenium.common.exceptions.StaleElementReferenceException: Message:
stale element reference: element is not attached to the page document
(Session info: chrome=71.0.3578.98) (Driver info:
chromedriver=2.45.615291
(ec3682e3c9061c10f26ea9e5cdcf3c53f3f74387),platform=Windows NT
10.0.17134 x86_64)
Was Selenium not install properly? or is the error inside my code? I would really appreciate any help I can get.
Btw: I'm finding the username and password elements by name and not xpath, since Instagram changes the xpath and selector every time the page is opened, making the elements quite hard to find in other ways, at least with my very limited experience.
This image shows what the browser looks like after the script is tested:
Use WebDriver Wait :
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,
"input[name='username']"))).send_keys("test")
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,
"input[name='password']"))).send_keys("test")
or
user = browser.find_element_by_xpath("//input[#name='username']")
user.send_keys('test')
passw = browser.find_element_by_xpath("//input[#name='password']")
passw.send_keys('test')'
Hope this will work!
it works with my code, if it's helpful please accept it.
1 add a expected_conditions wait util the actual login page appear
second_page_flag = wait.until(EC.presence_of_element_located(
(By.CLASS_NAME, "KPnG0"))) # util login page appear
2 change the xpath selector syntax here
login_button_ = browser.find_element_by_xpath(
"//form[#class='HmktE']/div[3]/button")
login_button_.click()
the all codes here:
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
browser = webdriver.Chrome()
browser.get('https://www.instragram.com')
wait = WebDriverWait(browser, 10)
login_elem = browser.find_element_by_xpath(
'//*[#id="react-root"]/section/main/article/div[2]/div[2]/p/a')
second_page_flag = wait.until(EC.presence_of_element_located(
(By.CLASS_NAME, "KPnG0"))) # util login page appear
user = browser.find_element_by_name("username")
passw = browser.find_element_by_name('password')
ActionChains(browser)\
.move_to_element(user).click()\
.send_keys('test')\
.move_to_element(passw).click()\
.send_keys('test')\
.perform()
login_button_ = browser.find_element_by_xpath(
"//form[#class='HmktE']/div[3]/button")
login_button_.click()
Try this code.It worked for me.No action class required.Hope this will help you.Any issues please reply back.You need to download latest chrome driver ChromeDriver 2.45 from http://chromedriver.chromium.org/downloads
from selenium import webdriver
import time
driver=webdriver.Chrome("Path of the chromdriver location" + "chromedriver.exe" )
driver.get("https://www.instagram.com/accounts/login/?source=auth_switcher")
user=driver.find_element_by_name("username")
user.send_keys("kajal")
passwd=driver.find_element_by_name("password")
passwd.send_keys("1234")
time.sleep(1)
button=driver.find_element_by_xpath(".//*[#id='react-root']/section/main/div/article/div/div[1]/div/form/div[3]/button")
button.click()
I had the same issue. The problem is that python tries to find the element immediately but the webdriver didn't load the element yet. To fix the problem make the program wait for the elements to load and only then call them. You can do this by importing time and adding: time.sleep(4). So that would be:
import time
from selenium import webdriver
browser = webdriver.Chrome("/usr/lib/chromium-browser/chromedriver")
browser.get("https://www.instagram.com/accounts/login/")
time.sleep(4) #so you let the webpage load
browser.find_element_by_name("username").send_keys("blablabla")
For what it's worth, there's a typo in the code provided in original post: " browser.get('https://www.instRAgram.com') ". Looks like the page redirects to main Insta page, so maybe your code is trying to scrape the redirect page or something like that making it buggy.

Python Selenium chrome driver .send_keys NoneType Error

I am trying to enter a username into an input box but it is not working. The site opens and I can get it to click on the box, but not words show up. The chrome developer extensions warning popup appears. I'm still fairly new to programming. Any ideas? Thanks
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
browser = webdriver.Chrome()
browser.get('https://login.live.com/login.srf?wa=wsignin1.0&rpsnv=13&ct=1478123673&rver=6.7.6631.0&wp=MBI&wreply=https%3a%2f%2fwww.bing.com%2fsecure%2fPassport.aspx%3frequrl%3dhttp%253a%252f%252fwww.bing.com%252f%253fwlexpsignin%253d1&lc=1033&id=264960')
time.sleep(5)
usernameElem = browser.find_element_by_class_name('phholder').click()
usernameElem.send_keys('username')
Error
Traceback (most recent call last):
File "C:/Users/Matt/PycharmProjects/untitled/Bing.py", line 10, in <module>
usernameElem.send_keys(Keys.ENTER)
AttributeError: 'NoneType' object has no attribute 'send_keys'
Your selector is wrong try using xpath or id:see below code
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
browser = webdriver.Chrome()
browser.maximize_window()
browser.get('https://login.live.com/login.srf?wa=wsignin1.0&rpsnv=13&ct=1478123673&rver=6.7.6631.0&wp=MBI&wreply=https%3a%2f%2fwww.bing.com%2fsecure%2fPassport.aspx%3frequrl%3dhttp%253a%252f%252fwww.bing.com%252f%253fwlexpsignin%253d1&lc=1033&id=264960')
time.sleep(5)
usernameElem = browser.find_element_by_xpath(".//*[#id='i0116']")
#Either use above xpath or use id
#usernameElem = browser.find_element_by_id("i0116")
usernameElem.send_keys('username')
you should perform send_keys function to an element
So that first assign the element to the variable usernameElem and after that click it as a function, then you will be able to send kes to the usernameElem element
usernameElem = browser.find_element_by_class_name('phholder')
usernameElem.click()
usernameElem.send_keys('username')

new tab not loading the URL in internet explorer using selenium and python

I am trying to open 2 tabs in IE( IE 11) using selenium webdriver and python(3.4).
here is the code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time
driver = webdriver.Ie()
#driver = webdriver.Chrome()
driver.get("http://www.google.com/")
window_before = driver.window_handles[0]
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 't')
time.sleep(3)
window_after = driver.window_handles[1]
driver.switch_to_window(window_after)
driver.get('http://stackoverflow.com/')
time.sleep(3)
Now this code is working fine in chrome. but in IE it opens new tab but doesn't load the stackoverflow URL.
I am getting this error:
Traceback (most recent call last):
File "C:\Python34\new_tab.py", line 12, in <module>
window_after = driver.window_handles[1]
IndexError: list index out of range
What is wrong?

Categories

Resources