如何使硒识别带有撇号的搜索

时间:2019-09-23 23:23:00

标签: python selenium

我制作了一个脚本,可以将学生成绩和评论从我的Google表格上传到D2L。 为了让硒进入具有用于评论的文本框的iframe,我搜索了名字和姓氏。去年这对我来说都是有效的。今年,我的名字中包括'作为姓氏的一部分,例如james d'arcy。硒不喜欢这样。

是否有一种简单的方法可以识别撇号?还是不需要完全匹配进行搜索的方法?

 for i in toplist:  # copies and pastes in comments 
        icnFeedback = "//a[contains(@title,'"+ i[0]+"') and @class='d2l-imagelink']" #i[0] is students name, looking that it is contained in title. 
        comments = i[1]  # i[1] is student comment

        wait.until(EC.element_to_be_clickable((By.XPATH, icnFeedback)))
        myElement = driver.find_element_by_xpath(icnFeedback)   # find user by names
        driver.execute_script("arguments[0].click();", myElement)   #clicks the feedback button

1 个答案:

答案 0 :(得分:1)

似乎正在发生这种情况,因为您使用单引号来包含字符串(i[0]),并且字符串本身中也有一个单引号。发生这种情况时,查询将失去其结构。

fake_ln = "las'tname"
xpath = "//a[contains(@title,'"+ fake_ln +"') and @class='d2l-imagelink']"
print(xpath)
# outputs: //a[contains(@title,'las'tname') and @class='d2l-imagelink']

这是一个问题,因为现在您不仅在引号中使用了姓氏,而且还将) and @class=放在了引号中。

尝试一下:

icnFeedback = '//a[contains(@title,"'+ i[0]+'") and @class="d2l-imagelink"]'