当我在selenium中使用execute\u script命令时,我不断收到SyntaxError

2024-03-28 20:22:07 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我使用Python Selenium来实现一些自动化,我使用了一个execute\u脚本来单击,下面是我的代码片段:

        WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, '//*[@id="nav-all"]/ul/li[' + str(x+1) + ']/div[2]/div/h5/a')))
        song_title = driver.find_element_by_xpath('//*[@id="nav-all"]/ul/li[' + str(x+1) + ']/div[2]/div/h5/a').text
        WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, '//*[@id="nav-all"]/ul/li[' + str(x+1) + ']/div[2]/div/div')))
        song_author = driver.find_element_by_xpath('//*[@id="nav-all"]/ul/li[' + str(x+1) + ']/div[2]/div/div').text
        if audio_author.split(",")[0].strip() in song_author and song_title in audio_title:
            WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, song_title))
            driver.execute_script('arguments[0].click();', driver.find_element_by_link_text(song_title))

我尝试单击的最后一行是遇到错误的地方,但有趣的是,当我用另一行不产生错误的代码替换代码时,仍然会发生语法错误。错误是因为代码周围的行造成的吗

这是我试图替换最后一行的行(在使用它的地方没有引发错误,但是在替换最后一行时引发错误)

    driver.execute_script("arguments[0].click();", driver.find_element_by_xpath('//*[@id="form-login"]/div[1]/div/div/button'))

Tags: 代码dividbysongtitledriver错误