Python Forum
Python Selenium .click() Loads Error - Works Manually.- Events not Triggered
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Selenium .click() Loads Error - Works Manually.- Events not Triggered
#11
Would I get the cookies from the page before clicking the submit button (step 3) or when I launch Chrome (step 2)?

here are the steps I take

1) Selenium Launches Chrome -> 2) Navigate to Login Screen -> 3) Navigate to Page with Submit Button (html below)

<div class="mc2" id="user_input">   
                 
<form id="request" name="request" onsubmit="return checkAndSubmit(this.tNumber.value)" action="/ebilling/request.action" method="post">
    
                <input type="hidden" name="CSRFToken" value="8be3e045-99b4-46f6-a6d9-f34e70017b96">
        <table class="modulebackgroundcolor" width="100%" cellspacing="0" cellpadding="0" border="0">  
            <tbody><tr>
                <td>X. <a href="https://www.x.com/assets/resources/media/en_US/terms_service_us.pdf" target="_blank">x</td>
            </tr>
            <tr>
                <td>Please enter the TNumber (XXXXXXXXXXXXXXX or 99999999999) below to submit your request.</td>
            </tr>         
            <tr>              
                <td id="errorMessage" style="color: red;">
                 
                                     
                </td>             
            </tr>
            <tr>
                <td>
     
 
<input type="text" name="tNumber" size="30" maxlength="30" value="" id="request_tNumber"></td>
            </tr>
            <tr>
                <td>Note: XYZ.</td>
            </tr>
            <tr>
                <td>*XXX</td>
            </tr>
            <tr>
                <td>&nbsp;</td>
            </tr>         
        </tbody></table>
        <div class="mb" align="right">
        <h4>
            <span><input class="bSwan" type="button" value="Cancel" onclick="location.href='/ebilling/dashBoard.action?reportId=dashboardIncentiveRpt'"></span>
            <span><input type="submit" value="Submit" id="request_0" class="bSwan">
</span>
        </h4>
        </div>
        <br>      
    <input type="hidden" name="_csrf" value="e7634769-8942-415f-b939-39e748359b81"></form>
 
 
    
    </div> 
Reply
#12
I would do it right after logging in
Recommended Tutorials:
Reply
#13
Hi metulburr,

I am just now getting back to this project and I attempted the code you posted, but I get the following error:

Traceback (most recent call last):
  File "C:/Users/SA/PycharmProjects/xyz.py", line 96, in <module>
    c = [driver.add_cookie(c) for c in response_cookies_browser]
  File "C:/Users/SA/PycharmProjects/xyz.py", line 96, in <listcomp>
    c = [driver.add_cookie(c) for c in response_cookies_browser]
  File "C:\Users\SA\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 894, in add_cookie
    self.execute(Command.ADD_COOKIE, {'cookie': cookie_dict})
  File "C:\Users\SA\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\SA\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.InvalidArgumentException: Message: invalid argument: missing 'value'
  (Session info: chrome=75.0.3770.142)
  (Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}),platform=Windows NT 6.1.7601 SP1 x86_64)
Would you please take a look and advise what I might be doing wrong.

        url = "https://www.xyz.com"
        driver.get(url)
        driver.maximize_window()
        driver.find_element_by_id("email").send_keys(cca_username)
        driver.find_element_by_id("pwd").send_keys(cca_password)
        driver.find_element_by_id("submitBtn").click()

        #######
        request_cookies_browser = driver.get_cookies()
        params = {'os_username': 'username', 'os_password': 'password'}
        s = requests.Session()
        c = [s.cookies.set(c['name'], c['value']) for c in request_cookies_browser]
        resp = s.post(url, params)
        dict_resp_cookies = resp.cookies.get_dict()
        response_cookies_browser = [{'name': name, 'value': value} for name, value in dict_resp_cookies.items()]
        c = [driver.add_cookie(c) for c in response_cookies_browser]
Thank you
Reply
#14
does this return anything?
dict_resp_cookies.items()
Recommended Tutorials:
Reply
#15
dict_items([('Secure', None)])
Reply
#16
metulburr,

Just want to check in and see if you had anymore advice.

Thanks
Reply
#17
At this point my guess is just as good as yours. Its hard to identify a problem with selenium unless i can actually run the full code, and tinker with it on the fly. And even then i can still be just as stumped as you are. Ive never came across your issue before when using selenium. Is this a site you are able to make a dummy account, enough to mimic the problem, and pass that login info here?
Recommended Tutorials:
Reply
#18
Hi i have seen your PM,i do not do help with TeamViewer.
As mention bye @metulburr it's hard to help when not can test this live
In theory should CSRF token be send automatically as Selenium WebDriver just automated a user using the browser.

Just to show a basic log in example with Selenium using this site.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup
import time

#--| Setup
options = Options()
#options.add_argument("--headless")
browser = webdriver.Chrome(executable_path=r'C:\cmder\bin\chromedriver.exe', options=options)
#--| Parse or automation
browser.get('https://python-forum.io/member.php?action=login')
user_name = browser.find_elements_by_xpath("//input[@name='username']")[0]
user_name.send_keys("snippsat")
password = browser.find_elements_by_xpath("//input[@name='password']")[0]
password.send_keys("xxxxxxxx")
browser.implicitly_wait(2)
browser.find_elements_by_xpath('//*[@id="content"]/div/form/div/input')[0].click()
browser.implicitly_wait(2)
soup = BeautifulSoup(browser.page_source, 'lxml')
welcome = soup.find('span', class_="welcome").text
print(welcome)
Output:
Welcome back, snippsat. You last visited: Today, 02:46 PM Log Out
Same with Request in this post.
Reply
#19
I would like to add that this is not a login issue. This is afterwords; putting in a number to search while being logged in. Login works successfully. I can verify that manually clicking the search button works, while when doing it selenium creates an error on the website (if you go back a page after the error you can click it manually and works). The website somehow detects when selenium clicks it? The button Id is correct, it correctly clicks it, there is no iframe, etc. It does feel like there is some sort of javascript detecting that the browser is automated (not sure if that is possible or not) and blocking it because of that.

The confusion lies as to if selenium was being blocked...wouldnt they block you at login instead? Why do it there when they could of blocked you from logging in completely. However there is a method to check if that is the case.

@OP
1) Do you need to be logged in to put in that number and search?
2) Can you create a dummy account there to hand it over publicly here?
3) Teamviewer is kind of laggy. And its quite uncomfortable doing it there when if i had code i could do it leisurely on my own time, even sporadically.
Recommended Tutorials:
Reply
#20
Thank you snippsat for the reply! and again, metlburr, you're awesome -- thank you very much for your attempts so far.

1) Do you need to be logged in to put in that number and search?

Yes, you need to be logged in.

2) Can you create a dummy account there to hand it over publicly here?

I am going to attempt to do this. Right now I am working with a client's login information, which is why I have not shared it.

3) Teamviewer is kind of laggy. And its quite uncomfortable doing it there when if i had code i could do it leisurely on my own time, even sporadically.

I completely understand, and normally wouldn't have asked for assistance like this. It's just the client information and I didn't feel comfortable sharing it. I actually tried to create a dummy account before asking for assistance using TeamViewer, but I couldn't create one (I will try again).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Click on a button on web page using Selenium Pavel_47 7 4,564 Jan-05-2023, 04:20 AM
Last Post: ellapurnellrt
  Show HTML in Python application and handle click SamHobbs 2 2,660 Sep-28-2021, 06:27 PM
Last Post: SamHobbs
  Selenium/Helium loads up a blank web page firaki12345 0 2,007 Mar-23-2021, 11:51 AM
Last Post: firaki12345
  button click error rafarangel 2 3,082 Feb-11-2021, 08:19 PM
Last Post: buran
  Log In Button Won't Click - Python Selenium Webdriver samlee916 2 3,770 Jun-07-2020, 04:42 PM
Last Post: samlee916
  Hyperlink Click is not working in Selenium webdriver rajeev1729 0 1,994 May-02-2020, 11:21 AM
Last Post: rajeev1729
  selenium click in iframe fails 3Pinter 6 5,022 Apr-29-2020, 12:59 PM
Last Post: Larz60+
  Selenium weird error julio2000 0 1,646 Feb-23-2020, 01:24 PM
Last Post: julio2000
  Selenium webdriver error WiPi 4 11,978 Feb-09-2020, 11:38 AM
Last Post: WiPi
  error when running headless selenium julio2000 2 4,507 Feb-01-2020, 12:41 PM
Last Post: julio2000

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020