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

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

最佳答案

如前所述, "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()

关于python - 标识符中的无效字符 (Python3),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41841690/

10-12 13:44