【问题标题】:Invalid character in identifier (Python3)标识符中的无效字符(Python3)
【发布时间】:2017-06-10 01:10:27
【问题描述】:

我不明白无效字符在哪里,它指向代码的末尾。

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
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.webdriver.common.keys import Keys


def page_is_loaded(driver):
   return driver.find_element_by_tag_name("body") != None

driver = webdriver.Chrome()
driver.get("https://www.adidas.co.uk/on/demandware.store/Sites-adidas-GB-Site/en_GB/MyAccount-Register")
year = wait.until(EC.visibility_of_element_located((By.XPATH,'//spa‌​n[@data-val="1990"]'‌​)))
driver.execute_script('arguments[0].scrollIntoView(true);', year)
                                                                            ^
SyntaxError: invalid character in identifier

【问题讨论】:

  • 欢迎来到 StackOverflow。请阅读并遵循帮助文档中的发布指南。 Minimal, complete, verifiable example 适用于此。在您发布 MCVE 代码并准确描述问题之前,我们无法有效地帮助您。错误消息通常会在语法上最近的行上标记某些内容;我们需要足够的代码来重现问题。
  • 很抱歉。
  • 括号中的"1990"]'‌​))) 末尾似乎有一些奇怪的字符。它在解释器中显示为"]'\U+200C​)))。尝试用 '))) 替换它们,复制并粘贴。至少这为我清除了错误。
  • 有点工作,但下拉菜单没有向下滚动。

标签: python google-chrome selenium automated-tests


【解决方案1】:

如前所述,"1990"]' 中有一个隐藏符号。当您从 StackOverflow 复制代码时,有时会发生这种情况。你只需要重新输入这部分。

如果您需要向下滚动到目标选项 (1990),请使用

import time

driver = webdriver.Chrome()
wait = WebDriverWait(driver, 10)
driver.get("https://www.adidas.co.uk/on/demandware.store/Sites-adidas-GB-Site/en_GB/MyAccount-Register")
driver.switch_to_frame(driver.find_element_by_xpath('//iframe[@class="sso-iframe"]'))
wait.until(EC.presence_of_element_located((By.XPATH,'//a[.="YY"]'))).click()
year = wait.until(EC.presence_of_element_located((By.XPATH,'//li[span[@data-val="1990"]]' )))
year.location_once_scrolled_into_view
time.sleep(1)
year.click()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多