使用硒执行事件和报废

时间:2019-05-03 14:25:17

标签: python selenium selenium-webdriver

我对硒很陌生,我期待执行一个click()按钮来扩展表格,此后我希望取消扩展表格的内容。

这是我尝试过的:

url = 'https://www.telegraph.co.uk/markets-hub/assets/shares/'
phantomjs_path = '/usr/local/bin/phantomjs'
driver = webdriver.PhantomJS(executable_path=phantomjs_path,service_args=['--load-images=no'])
driver.get(url)

element = driver.find_element_by_xpath("//div[@class='tmg-show-more']")

html_source = driver.page_source
element.click()
driver.quit()
soup = BeautifulSoup(html_source,"html5lib")
tables=soup.find('table',class_="table-static kurser-table")
link=pd.read_html(str(tables),flavor='html5lib',thousands='.',decimal=',',header=0)



print(link)

当前输出如下:

raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: {"errorMessage":"Element is not currently visible and may not be manipulated",
"request":{"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"81","Content-
Type":"application/json;charset=UTF-8","Host":"127.0.0.1:58447","User-Agent":"Python http auth"},
"httpVersion":"1.1","method":"POST","post":"{\"id\": \":wdc:1556893378248\", \"sessionId\": \"f223cb80-6dae-11e9-b00b-4bb158952171\"}",
"url":"/click","urlParsed":{"anchor":"","query":"","file":"click","directory":"/","path":"/click","relative":"/click","port":"","host":"","password":"","user":"",
"userInfo":"","authority":"","protocol":"","source":"/click","queryKey":{},"chunks":["click"]},"urlOriginal":"/session/f223cb80-6dae-11e9-b00b-4bb158952171/element/:wdc:1556893378248/click"}}
Screenshot: available via screen

2 个答案:

答案 0 :(得分:1)

您将获得ElementNotVisibleException表示您试图在元素可见之前单击它。

此外,在单击按钮之前,您正在获取页面源。单击并加载新数据后,您需要获取源。

在获取页面源代码之前,请尝试以下代码。

wait = WebDriverWait(driver, 20)
element = wait.until(EC.element_to_be_clickable(driver.find_element_by_xpath("//div[@class='tmg-show-more']")))
element.click()
wait.until(EC.invisibility_of_element(element))

要使用WebDriverWait,您需要导入这些

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

答案 1 :(得分:1)

您需要向下滚动以让Selenium Web驱动程序知道该元素实际存在的位置。

代码:

driver.get("https://www.telegraph.co.uk/markets-hub/assets/shares/")

wait = WebDriverWait(driver, 10)

wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'div.section-box.kurser-table-container')))

element = driver.find_element_by_xpath("//a[@ng-click='stocksShowMore()']")

actions = ActionChains(driver)
actions.move_to_element(element).perform()

element.click()

进口:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC 
from selenium.webdriver.common.action_chains import ActionChains