Navigating through pages using selenium python - python

def get_link(search_term,page):
try:
Pinbox = driver.find_element_by_xpath('//*[#id="container"]/div/div[1]/div[1]/div[2]/div[3]/div[2]/div/div[2]/div[2]/div/div/div[1]/form/input')
Pinbox.send_keys(Pincode)
Pinbox.submit()
except exceptions.NoSuchElementException:
return
try:
grocery="https://www.flipkart.com/search?q={}&otracker=search&otracker1=search&marketplace=GROCERY&as-show=on&as=off"
search_term = search_term.replace(' ', '+')
stem = grocery.format(search_term)
url_template = stem + '&as-pos=1&as-type=HISTORY&as-backfill=on&page='
next=url_template+str(page)
print(next)
if page > 1:
return next
else:
return Pinbox and stem
except exceptions.NoSuchElementException:
return
def PID():
for page in range(1,20):
path=get_link(term,page)
driver.get(path)
id=driver.find_elements_by_xpath('//div[#data-id]')
for i in id:
results=i.get_attribute('data-id')
print(results)
FSN_List.append(results)
PID()
unable to navigate through pages,get an error selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: 'url' must be a string

Exception appears, because you return None, from get_link function. In get_link function first step is finding element, but you have not moved to the site yet:
try:
Pinbox = driver.find_element_by_xpath('//*[#id="container"]/div/div[1]/div[1]/div[2]/div[3]/div[2]/div/div[2]/div[2]/div/div/div[1]/form/input')
Pinbox.send_keys(Pincode)
Pinbox.submit()
except exceptions.NoSuchElementException:
return
you should first provide link to .get() driver funciton, thand then looking for elements

i have created a different function,
def pin(Pincode):
try:
Pinbox = driver.find_element_by_xpath('//*[#id="container"]/div/div[1]/div[1]/div[2]/div[3]/div[2]/div/div[2]/div[2]/div/div/div[1]/form/input')
Pinbox.send_keys(Pincode)
Pinbox.submit()
except exceptions.NoSuchElementException:
return
this resolved the issue

Related

How to fix dictionary, get is not a known member of None in Pylance language server prompt message

Love to use pylance, but unable to fix this issue in dictionary get, pylance prompt message as shown in the image
def validate_amount():
try:
json_data = request.json
def isfloat(num: str) -> bool:
try:
float(num)
return True
except ValueError:
return False
amount_val = json_data.get('amount','0.0')
amount_in_words = json_data.get('amount2words','')
if isfloat(amount_val):
split_amt = amount_val.split('.')
response = split_amt
except Exception as e:
response = f'An Error Occurred - {e}'
Below is the image for reference

want to Skip the error lines of code selenium python try/except

Hi want to Skip the error lines of code as it is Unable to locate element.
Piece of code given below , Please find profile_cover line is given error. I have added try except block to handle it still is it giving me elenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element error. Please advise
else:
body = driver.find_elements_by_xpath("//*[#class='feed']")
post_type = 'Personal Profile'
if len(body) > 0:
body = body[0]
post_tile = body.find_elements_by_xpath(".//*[#class='_55wo _5rgr _5gh8 async_like _1tl-']")
print('its a normal profile page')
try:
profile_cover = driver.find_element_by_xpath("//*[#class='_7g4m']//*[#class='_7g2i _48kp']/a/i")
**// GETTING ERROR HERE AND WANT TO SKIP THIS**
ilink = profile_cover.get_attribute('style')
ilink = ilink.split('(')[1]
ilink = ilink.split(')')[0]
print(ilink)
profile_info.append(ilink)
profile_ph = driver.find_element_by_xpath(
"//*[#class='_52jj _42b3']//i[#class='img profpic' or #class='img _1-yc profpic']")
ilink = profile_ph.get_attribute('style')
ilink = ilink.split('(')[1]
ilink = ilink.split(')')[0]
print(ilink)
profile_info.append(ilink)
except ValueError as e:
print(e)
try this instead :
try:
profile_cover = driver.find_element_by_xpath("//*[#class='_7g4m']//*[#class='_7g2i _48kp']/a/i")
except NoSuchElementException as exc:
print(exc)
Make sure to import these :
from selenium.common.exceptions import TimeoutException, WebDriverException, NoSuchElementException

How to write exception for ProxyConnetionError (errno - 10060, can't connect to proxy) for a Django project

Can anyone please help me how to write exception for a ProxyConnectionError? I have tried following code in Django project after browsing internet
from aiohttp_socks import ProxyConnectionError
try:
return render(request,"data.html")
except ProxyConnectionError:
return HttpResponse("{Exception: check proxy settings}")
I solved it by using below code
def user(request):
try:
c = twint.Config()
c.Username = "twitter"
c.Limit=20
c.Hide_output = True
c.Store_json=True
c.Proxy_host = myresult[1]
c.Proxy_port = myresult[2]
c.Proxy_type = "socks5"
asyncio.set_event_loop(asyncio.new_event_loop())
twint.run.Search(c)
return render(request,"data.html")
except ProxyConnectionError as e:
return HttpResponse(e)

AttributeError: instance has no attribute error in Linux only

I am getting the AttributeError: PortalTesting instance has no attribute 'reporting_screenshot' error in Linux only. When I run this code on Windows, its working as expected.
#!/usr/bin/env python
class PortalTesting:
def __init__(self):
self.start_time = time.time()
self.status = OrderedDict()
self.screenshots_folder = ''
self.new_folder_name = ''
self.tday = datetime.today().strftime('%Y%m%d-%H%M')
self.dt = date.today() - timedelta(2)
self.start_date = self.dt.strftime('%m-%d-%Y')
# self.reporting_screenshot = ''
# self.transcript = ''
def login(self):
try:
URL = ''
driver.get(URL)
# ------------------- Transcript ------------------- #
def transcript(self):
try:
tr_e1 = driver.find_element_by_id('Transcript_ID')
hover1 = ActionChains(driver).move_to_element(tr_e1)
hover1.click().perform()
....
....
self.transcript_screenshot = self.path + self.tday + "_Transcripts.png"
driver.get_screenshot_as_file(self.transcript_screenshot)
self.status['Transcript'] = ["Pass", self.transcript_screenshot]
# THIS IS WHERE I GET ERROR
except:
self.status['Transcript'] = ["Fail", self.transcript_screenshot]
# ------------------- Reporting ------------------- #
def reporting(self):
try:
# Reports tab
rpt_e1 = driver.find_element_by_id('report_menu_id')
hover1 = ActionChains(driver).move_to_element(rpt_e1)
hover1.click().perform()
.....
.....
WebDriverWait(driver, 60).until(EC.element_to_be_clickable((By.ID, 'PortalChartContainer'))).click()
self.reporting_screenshot = self.tday + "_Reports.png"
driver.get_screenshot_as_file(self.path + self.reporting_screenshot)
print("Reporting check complete.")
self.status['Reporting'] = ["Pass", self.reporting_screenshot]
# THIS IS WHERE I GET ERROR
except:
self.status['Reporting'] = ["Fail", self.reporting_screenshot]
pt = PortalTesting()
pt.login()
pt.reporting()
pt.transcript()
pt.admin()
pt.print_status()
What I am doing here is, login to URL. Run reports/ transcript. If success, add Pass and screenshot to self.status dictionary. If fails, add Fail and screenshot to self.status dictionary.
Above code works fine on Windows but, gives error when I copy paste same code on Linux.
If I add self.transcript = '' and self.reporting_screenshot = '' in __init__, I get TypeError: 'str' object is not callable
Traceback Error:
Traceback (most recent call last):
File "DC1PortalTesting.py", line 477, in <module>
pt.reporting()
File "DC1PortalTesting.py", line 375, in reporting
self.status['Reporting'] = ["Fail", self.reporting_screenshot]
AttributeError: PortalTesting instance has no attribute 'reporting_screenshot'
I would really appreciate your help.
#merlyn is right: the first error arise because of you try/except statement in the definition of reporting(). If any error pops up before self.reporting_screenshot = self.tday + "_Reports.png", then the attribute does not exist, and the except clause will raise this error.
Two suggestions here:
self.reporting_screenshot should be create at first in the definition of reporting(), because all its components already exist at this point.
def reporting(self):
self.reporting_screenshot = self.tday + "_Reports.png"
try:
# Reports tab
rpt_e1 = driver.find_element_by_id('report_menu_id')
hover1 = ActionChains(driver).move_to_element(rpt_e1)
hover1.click().perform()
.....
.....
WebDriverWait(driver, 60).until(EC.element_to_be_clickable((By.ID, 'PortalChartContainer'))).click()
# Removed line here
driver.get_screenshot_as_file(self.path + self.reporting_screenshot)
print("Reporting check complete.")
self.status['Reporting'] = ["Pass", self.reporting_screenshot]
...
Catch the error in your except statement, for debugging purpose.
...
# THIS IS WHERE I GET ERROR
except Exception as e: # catches any exception
# print(e) # uncomment for debugging, or make it smarter with a switch
self.status['Reporting'] = ["Fail", self.reporting_screenshot]
Regarding the other error: #merlyn also provided an explanation in his comment.
Regarding why your code fails on Linux: I assume it is working on Windows so you never tested the except clause there, right?

Unbound local variable Python 3.2

This error pops up randomly, and I'm pretty sure it's because the infoGotten variable isn't initialized before the return statement calls it. The part that has me puzzled is how it's getting to that part of the code to produce that error in the first place. Hopefully someone can explain to me why that is as I haven't been able to figure it out yet. I'm guessing it's because of the try/except statement but I did some searching and checked 7.4 in the manual and it doesn't appear (to me anyways) that I'm doing something incorrect.
breakLoop = 0
def get_item_info(linkParameters):
global breakLoop
nheaders = {'User-Agent' : 'Firefox/15.0.1'}
purl = 'http://example.com/something.php'
pd = linkParameters
nreq = urllib.request.Request(purl, pd, nheaders)
if breakLoop >= 4:
return 'Request timed out {} times'.format(breakLoop)
try:
nresponse = urllib.request.urlopen(nreq)
except urllib.error.URLError:
breakLoop += 1
get_item_info(pd)
except urllib.error.HTTPError:
breakLoop += 1
get_item_info(pd)
else:
infoGotten = nresponse.read()
return infoGotten
Thanks!
You need to return the results of the recursive calls, so it should be return get_item_info(pd) in the except clauses (which I combined below):
breakLoop = 0
def get_item_info(linkParameters):
nheaders = {'User-Agent' : 'Firefox/15.0.1'}
purl = 'http://example.com/something.php'
pd = linkParameters
nreq = urllib.request.Request(purl, pd, nheaders)
if breakLoop >= 4:
return 'Request timed out {} times'.format(breakLoop)
try:
nresponse = urllib.request.urlopen(nreq)
except (urllib.error.URLError, urllib.error.HTTPError):
breakLoop += 1
return get_item_info(pd)
else:
return nresponse.read()
Recursion seems like a weird way to perform the retries though, why not use a loop? The following seems more clear:
def get_item_info(linkParameters):
nheaders = {'User-Agent' : 'Firefox/15.0.1'}
purl = 'http://example.com/something.php'
pd = linkParameters
for i in range(5):
nreq = urllib.request.Request(purl, pd, nheaders)
try:
nresponse = urllib.request.urlopen(nreq)
return nresponse.read()
except (urllib.error.URLError, urllib.error.HTTPError):
pass
return 'Request timed out 4 times'

Categories