Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SyntaxError: invalid syntax
#1
Hello guys, I dont understand why this is not working.

If I use this, works


	
def my_function(i):
	while i<a:
		print(f[i])
		i=i+1
		pass


	pass

my_function(0)




And if I use this is not working
def init_driver():
	driver=webdriver.Firefox(executable_path="C:\\Users\\Sabrina\\Desktop\\geckodriver-v0.26.0-win64\\geckodriver.exe")
	driver.wait=WebDriverWait(driver, 5)
	driver.get("www link")
	return driver


def my_function(i,driver):
	while i<a:
		element=driver.wait.until(EC.presence_of_element_located((By.XPATH,f[i]))
		driver.find_element_by_xpath(f[i]).click()
        ^ 
        SyntaxError: invalid syntax 


		element1=driver.wait.until(EC.presence_of_element_located((By.XPATH,g[i])))
		driver.find_element_by_xpath(g[i]).click()
		import time
		time.sleep(2)
		driver.back()
		print(f[i])
		i=i+1
		pass

	pass




driver=init_driver()
my_function(0,driver)   



The erro is in the line: driver.find_element_by_xpath(f[i]).click()
Reply
#2
Even your first example doesn't work - a and f aren't declared anywhere (which is the same for your second example). Please provide a complete, runnable example.

Also, what's with the pass statements? They seem unnecessary.
Reply
#3
You are right I have to past everything



from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException


a=0
f1="xpathID1"
f2="xpathID2"
f3="xpathID3"....

f=[f1,f2,f3...]

g1="xpathID1.1"
g2="xpathID2.1"
g3="xpathID3.1"....

g=[g1,g2,g3...]

def init_driver():
    driver=webdriver.Firefox(executable_path="C:\\Users\\Sabrina\\Desktop\\geckodriver-v0.26.0-win64\\geckodriver.exe")
    driver.wait=WebDriverWait(driver, 5)
    driver.get("www link")
    return driver
 
 
def my_function(i,driver):
    while i<a:
        element=driver.wait.until(EC.presence_of_element_located((By.XPATH,f[i]))
        driver.find_element_by_xpath(f[i]).click()
        ^ 
        SyntaxError: invalid syntax 
 
 
        element1=driver.wait.until(EC.presence_of_element_located((By.XPATH,g[i])))
        driver.find_element_by_xpath(g[i]).click()
        import time
        time.sleep(2)
        driver.back()
        i=i+1
        pass
 
    pass


try:
    c1=driver.find_element_by_xpath("xpathID1")
    a=a+1
    c2=driver.find_element_by_xpath("xpathID2")
    a=a+1
    c3=driver.find_element_by_xpath("xpathID3")
    a=a+1...
except NoSuchElementException as e:
	print("no more")



driver=init_driver()
my_function(0,driver)   



this is the code
and the erro is in :driver.find_element_by_xpath(f[i]).click()
inside in the: def my_function(i,driver):

And your question about : pass
I dont know why I have, actually I dont now what mean, I think phyton made when I create the def and while.
Reply
#4
on line 10 (in the first) / line 29 (in the second example) you have missing closing bracket.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(Mar-26-2020, 10:44 AM)windows11 Wrote: I dont know why I have, actually I dont now what mean, I think phyton made when I create the def and while.
probably your IDE is creating a placeholder block for your function, i.e.
def foo():
    pass
is a valid function and it will not raise error. You are supposed to replace pass with actual code of the function when you are ready. The idea is you can crate a "dummy" function and later on you actually implement it.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
REALLY!!
Thanks a lot
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  print(data) is suddenly invalid syntax db042190 6 1,183 Jun-14-2023, 02:55 PM
Last Post: deanhystad
  SyntaxError: invalid syntax ?? korenron 15 5,710 Jan-25-2022, 11:46 AM
Last Post: korenron
  Invalid syntax with an f-string Mark17 7 7,777 Jan-14-2022, 04:44 PM
Last Post: Mark17
  invalid syntax in my class CompleteNewb 2 1,892 Dec-13-2021, 09:39 AM
Last Post: Larz60+
Exclamation Invalid syntax error(Predict Ethereum Price) lulu43366 2 3,164 Sep-24-2021, 01:24 PM
Last Post: lulu43366
  Unexplained Invalid syntax Error cybertooth 5 3,243 Aug-02-2021, 10:05 AM
Last Post: cybertooth
  [split] SyntaxError: invalid syntax Code_X 3 2,752 May-04-2021, 05:15 PM
Last Post: Yoriz
  Invalid syntax error - need help fixing calgk01 3 3,273 Feb-23-2021, 08:41 PM
Last Post: nilamo
  Invalid syntax using conditionals if - else jperezqu 1 2,331 Jan-13-2021, 07:32 PM
Last Post: bowlofred
  invalid syntax in line 5. Help Asadzangibaloch 2 2,386 Dec-10-2020, 04:26 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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