Reputation: 9
When running the following code, I receive a syntax error, but can't figure why:
import pytest
from selenium import webdriver
@pytest.fixture(scope="class")
def setup(request):
global driver
browser_name=request.config.getoption("browser_name")
if browser_name == "chrome":
driver = webdriver.Chrome(executable_path="C:\webdrivers\chromedriver.exe")
elif browser_name == "firefox":
driver = webdriver.Firefox(executable_path="C:\webdrivers\geckodriver.exe")
elif browser_name == "IE":
print("IE driver")
driver.get("https://ign.com/wikis/")
driver.implicitly_wait(10)
iframe = driver.find_element_by_xpath("//iframe[@name='__tcfapiLocator']")
driver.switch_to.frame(iframe)
driver.find_element_by_css_selector(#_evidon-banner-acceptbutton).click()
driver.switch_to.default_content()
request.cls.driver = driver
yield
driver.close()
Syntax error is as follows:
ImportError while loading conftest 'U:\test\conftest.py'.
C:\Program Files\Python39\lib\ast.py:50: in parse
return compile(source, filename, mode, flags,
E File "U:\test\conftest.py", line 38
E request.cls.driver = driver
E ^
E SyntaxError: invalid syntax
Process finished with exit code 4
Empty suite
If I comment out the iframe sections, this test runs without issue
I'm guessing this is a 'can't see the woods for the trees' scenario
Thanks in advance for any help.
Upvotes: 0
Views: 511
Reputation: 83537
"Invalid syntax" typically means you are missing punctuation or breaking some syntax rule. To find the problem, start where the error is shown and work backwards. Let's look at the previous few lines prior to the one that reports the syntax error:
driver.find_element_by_css_selector(#_evidon-banner-acceptbutton).click()
driver.switch_to.default_content()
request.cls.driver = driver
The syntax highlighting from Stack Overflow provides a clue to the problem. You can see that driver.find_element_by_css_selector(
is in white while #_evidon-banner-acceptbutton).click()
is gray. Gray indicates a comment that starts with #
.
I assume that you mean #_evidon-banner-acceptbutton
to be the CSS selector. To fix the problem, you need to make this a string by surrounding it with quotes:
driver.find_element_by_css_selector("#_evidon-banner-acceptbutton").click()
Notice how the "#_evidon-banner-acceptbutton"
is green now because the surrounding ""
s indicate it is a string.
I strongly suggest that you enable syntax highlighting in your editor to help find problems like these on your own.
Upvotes: 2
Reputation: 29362
I see you are not using double quotes at this line :
driver.find_element_by_css_selector(#_evidon-banner-acceptbutton).click()
instead use this :
driver.find_element_by_css_selector("#_evidon-banner-acceptbutton").click()
Upvotes: 0