【问题标题】:Python how to write in triple quotes?Python如何用三引号写?
【发布时间】:2016-04-25 21:25:44
【问题描述】:

我真的不知道如何用这个标题来表达,但这就是我想要做的 所以基本上,我试图让客户或用户选择他想要使用的类,但我找不到这样做,因为它需要三引号

from selenium import webdriver

x = str("\"\"\"")
y = str("\"\"\"")
class_name = input("Class name: ")
driver.get("http://stackoverflow.com/questions/12094153/selenium-webdriver-find-element-by-xpath-on-webelement")
driver.find_element_by_xpath(x + class_name + y).click()

【问题讨论】:

  • 我不知道你想用这些三引号做什么,但它们只用于文字,class_name 不是。而且我在那里看不到任何execeval,所以也没有。这是XY Problem

标签: python python-2.7 python-3.x selenium


【解决方案1】:

保持简单,直接使用find_element_by_class_name()方法:

from selenium import webdriver  

class_name = input("Class name: ")
driver.get("http://stackoverflow.com/questions/12094153/selenium-webdriver-find-element-by-xpath-on-webelement")
driver.find_element_by_class_name(class_name).click()

【讨论】:

    【解决方案2】:

    为什么不使用find_element_by_css_selector?要使用 xpath 找到具有类的元素需要更多的努力。

    driver.find_element_by_css_selector('.' + class_name).click()
    

    顺便说一句,三引号字符串与问题无关。

    【讨论】:

    • 工作了,哇我已经尝试了几个小时了,谢谢
    • 好吧,明白了,无论如何要将两个函数相乘?这就是我正在使用的hastebin.com/jitetakuxi.py
    • @Ghost,乘法?只需使用find_element_by_css_selector('.' + class_name) / find_element_by_class_name(class_name) 之一。您不需要同时使用两者。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-12
    • 2020-07-26
    • 2015-09-08
    • 1970-01-01
    • 1970-01-01
    • 2019-11-02
    • 2011-12-08
    相关资源
    最近更新 更多