Python / Selenium我一直在收到错误

时间:2015-11-01 03:40:57

标签: python-2.7 selenium

这里是我的代码没有导入所有必要的驱动程序我试图运行这个但我一直在试着

indentation error:
ps C:/user/asus/temp > python clickex1.py
  file "clickex1.py" line 25
    try:

是否有人可以回答我的问题,我会非常感兴趣 还有人可以检查我的代码并尝试使其更好吗?

results = []
with open("al.txt") as inputfile:
    for line in inputfile:
       results.extend(line.strip().split(',')) 

x = random.choice(results)   

while True:
    driver = webdriver.firefox()
    time.sleep(10)
    driver.get(x)
    time.sleep(10)
       try:
            element = driver.find_element_by_id("vB_Editor_QR_iframe")
            found = element
       if found == False:       
            except NoSuchElementException:
            time.sleep(5)
       elif found == True:
            element.send_keys("example")
            time.sleep(60)
            driver.close()

1 个答案:

答案 0 :(得分:2)

由于缩进不正确,您收到错误消息。该错误非常明显。您还没有正确使用异常处理。

while True:
    driver = webdriver.firefox()
    time.sleep(10)
    driver.get(x)
    time.sleep(10)
       try:
            element = driver.find_element_by_id("vB_Editor_QR_iframe")
            found = element
       if found == False:       
            except NoSuchElementException:
            time.sleep(5)
       elif found == True:
            element.send_keys("example")
            time.sleep(60)
            driver.close()

while True:
    driver = webdriver.firefox()
    time.sleep(10)
    driver.get(x)
    time.sleep(10)
    try:
        element = driver.find_element_by_id("vB_Editor_QR_iframe")
        found = element
        if found == False:       
            raise NoSuchElementException
        elif found == True:
            element.send_keys("example")
            time.sleep(60)
            driver.close()
    except NoSuchElementException:
            time.sleep(5)