标识符中的字符无效(Python3)

时间:2017-01-25 00:57:54

标签: python google-chrome selenium automated-tests

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

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

1 个答案:

答案 0 :(得分:4)

正如所说,"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()