Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Replying to a Javascript/jQuery
#1
I am trying to automate a web response. My problem is that I am not a javascript/jQuery expert and I have found that this web page apparently wants a response via Ajax/jQuery. I do not understand how to set up python to respond via the javascript. I have all the entry data as a python dictionary but I am hoping someone can tell me how to then execute the javascript with that data. FWIW, I have attached the script, as a txt file, I believe is being executed by the submit button. TIA.

Attached Files

.txt   jquery.txt (Size: 146.92 KB / Downloads: 184)
Reply
#2
No that file is
(Nov-17-2021, 02:31 PM)gw1500se Wrote: FWIW, I have attached the script, as a txt file, I believe is being executed by the submit button. TIA.
No that file is the whole jQuery library,so not what's been executed but other smaller script use this library code to execute.
So you need to explain better,or give url adress or full code if want help.
Stuff like this can be difficult to figure out if not well versed in web-development and use of dev-tools
Reply
#3
Thanks. Back to square one. I used Firefox's dev tools and that is what it seemed to say was being executed. I'm not sure how to figure out exactly which function is being executed. I know the displayed URL after clicking the submit button is "https://www.donotcall.gov/report.html#step2".
Reply
#4
It call something in 18n.
If want automat this i would not bother to find out what it dos,but use Selenium
A example find selector of button an then can click on it.
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
url = "https://www.donotcall.gov/report.html"
browser.get(url)
time.sleep(3)
button_continue = browser.find_elements_by_css_selector('#MainContinueButton')
button_continue[0].click()
# Continue with fill all the form data
Reply
#5
Thanks. I was not aware of selenium. I'll have to read up on it.
Reply
#6
I've started playing with your suggestion of selenium and have run into a problem understanding. The page I am loading has javascripts to fill in the page. When I load the page with selenium none of the javascripts are executed. From what I have read so far I see selenium can execute injected javascript by name. I don't see how to tell it to execute embedded javascripts when the page loads.
Reply
#7
(Nov-19-2021, 05:35 PM)gw1500se Wrote: The page I am loading has javascripts to fill in the page. When I load the page with selenium none of the javascripts are executed.
This is wrong or lack in your understating,Selenium will load a full browser with full JavaScript support.
If i should continue some further it would be like this,is not the easiest page to start with as there is lot filling in form to do.
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
url = "https://www.donotcall.gov/report.html"
browser.get(url)
time.sleep(3)
button_continue = browser.find_elements_by_css_selector('#MainContinueButton')
button_continue[0].click()
# Continue with fill all the form data
time.sleep(3)
phone =  browser.find_elements_by_css_selector('#PhoneTextBox')
phone[0].send_keys("2514455669")
date = browser.find_elements_by_css_selector('#DateOfCallTextBox')
date[0].click()
date_1 = browser.find_elements_by_css_selector('td.ui-datepicker-days-cell-over.ui-datepicker-today > a')
date_1[0].click()
Reply
#8
Thanks. I think I see as I was just trying to load the page and print it out as a starting point. BTW, what is the purpose of the sleep calls?
Reply
#9
(Nov-20-2021, 02:49 PM)gw1500se Wrote: what is the purpose of the sleep calls?
To slow the interaction down this make it easier to see what going in the browser,there also Waits for waiting for stuff to get loaded.

So with Selenium can do testing of web-page and also web-scraping of page.
Can also run headless(not loading browser) and eg use BS as parser as in example here
Reply
#10
Thanks. I like that I can see what is going on during execution. However, I still have one last (I hope) problem. I am trying to select a dropdown item thus:
hour=browser.find_element_by_css_selector('#TimeOfCallDropDownList')
hour.select_by_value(hh)
This results in a traceback with the following error:
Output:
Traceback (most recent call last): File "./donotcall.py", line 134, in <module> hour.select_by_value(hh) AttributeError: 'FirefoxWebElement' object has no attribute 'select_by_value'
According to the documentation, that should work. I have not been able to find anything useful about this error. The only thing I found was a suggestion to use select_by_visible_text which in my case is going to be a pain as I would have to develop it.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  POST Reply to Ajax/jQuery (mostly an HTML question) gw1500se 5 2,606 Nov-18-2021, 02:44 PM
Last Post: gw1500se

Forum Jump:

User Panel Messages

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