Python Selenium :: Element目前是不可见的

时间:2016-07-30 08:03:20

标签: python selenium

所以我正在访问this链接并在this朋友的帮助下,我能够取得进展。现在我被卡住了。问题是,当有人点击Reply并点击Post按钮时,会显示Element is not currently visible and so may not be interacted with

代码如下:

import requests
from bs4 import BeautifulSoup
from gensim.summarization import summarize

from selenium import webdriver
from datetime import datetime
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
from time import sleep
import sys
import os
import xmltodict
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import traceback
import random

driver = None
driver = webdriver.Firefox()
driver.maximize_window()
        url = 'https://steemit.com/steem/@ozchartart/usdsteem-btc-technical-analysis-2-the-only-way-for-me-to-move-on-is-to-chart-it-in-the-dawn'

        driver.get(url)
        sleep(5)
        f = driver.find_element_by_css_selector('.PostFull__reply')
        location = f.location["y"] - 100
        driver.execute_script("window.scrollTo(0, %d);" % location)
        f.click()
        t = driver.find_element_by_tag_name('textarea')
        b = driver.find_element_by_tag_name('button')
        # b = WebDriverWait(driver, 20).until(
        #     EC.presence_of_element_located((By.TAG_NAME, "button"))
        # )
        # print(b)
        t.click()
        sleep(1)
        t.send_keys('awesome!!')
        sleep(2)
        driver.execute_script("arguments[0].style.visibility = 'visible'; arguments[0].style.height = '1px'; arguments[0].style.width = '1px'; arguments[0].style.opacity = 1",b)

        driver.execute_script('document.getElementsByTagName("button")[0].click();')
        #if b.is_displayed():
        b.click()
    except Exception as e:
        driver.save_screenshot('myscreen.png')
        print(str(e))

1 个答案:

答案 0 :(得分:1)

似乎您正在尝试与其他按钮互动。尝试更具体的选择器:

driver.find_element_by_xpath('//button[text()="Post"]').click()

这对我有用