我的脚本无法单击某个网格

时间:2018-07-15 07:45:10

标签: python python-3.x selenium selenium-webdriver web-scraping

我已经用python与硒结合编写了脚本,以从网页中获取一些信息。要获得内容,必须跨几步,如accepting conditionfill in the inputboxclick on the search button那样填充结果,最后单击the first grid(第一个tr,更具体地说是)。在the first tr上启动任何点击后,就会打开一个新页面(包含所需信息)。

我的脚本可以成功完成前三个步骤。我不能做的是:

  1. perform a click on the first tr
  2. focus to the newly opened tab(包含我所关注的信息)
  

要获取内容:

     

此为the link。首先有一个接受按钮。然后有一个输入框NameHMC DESIGN GROUP填充。现在,按搜索按钮,结果应显示在表格的下方。从那里,我需要单击第一个tr。

这是我到目前为止尝试过的:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

link = "https://officialrecords.broward.org/AcclaimWeb/search/SearchTypeName"

def get_information(driver,url):
    driver.get(url)
    wait.until(EC.element_to_be_clickable((By.ID, "btnButton"))).click()
    wait.until(EC.presence_of_element_located((By.ID,"SearchOnName"))).send_keys("HMC DESIGN GROUP")
    wait.until(EC.presence_of_element_located((By.ID, "btnSearch"))).click()
    wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,".t-grid-content table tr"))).click()

if __name__ == "__main__":
    driver = webdriver.Chrome()
    wait = WebDriverWait(driver,10)
    try:
        get_information(driver,link)
    finally:
        driver.quit()

当前,脚本既不单击网格the first tr of the newly generated table,也不引发任何错误。它会正常退出浏览器。

2 个答案:

答案 0 :(得分:1)

只需替换此 css选择器

.t-grid-content table tr 

.t-grid-content table tr.t-state-selected:first-child  

点击第一个 tr

在单击第一个tr之前,将窗口句柄存储为:

window_before = driver.window_handles[0]  

单击第一个tr后,将新打开的窗口的窗口句柄存储为:

window_after = driver.window_handles[1]  

现在您要做的就是将Web驱动程序的焦点切换到新打开的窗口。就像这样:

driver.switch_to_window(window_after)

答案 1 :(得分:1)

div下还有另一个"t-grid-content",具有相同的类名"Loading..."。它显示为".t-grid-content table tr"。因此,在提交搜索之后,您确实可以使用选择器wait.until(EC.element_to_be_clickable((By.XPATH, "//td[contains(., 'HMC DESIGN GROUP')]"))) 单击节点,但是这样做没有效果

您只需要更具体的选择器。

例如尝试

driver.get(url)
current = driver.current_window_handle
wait.until(EC.element_to_be_clickable((By.ID, "btnButton"))).click()
wait.until(EC.presence_of_element_located((By.ID,"SearchOnName"))).send_keys("HMC DESIGN GROUP")
wait.until(EC.presence_of_element_located((By.ID, "btnSearch"))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, "//td[contains(., 'HMC DESIGN GROUP')]"))).click()
driver.switch_to.window([window for window in driver.window_handles if window != current][0])

要切换到新窗口,请尝试将功能主体更新为

text = ['this is the first sentence of the first paragraph. and this is the second sentence.','some random text in the second paragraph. and another test sentence.','here is the third paragraph. and this is another sentence','I have run out of text here. I am learning python and deep learning.','another paragraph with some random text. the this is a learning sample.','I need help implementing word2vec. this all sounds exciting.','it''s sunday and I shoudnt be learning in the first place. it''s nice and sunny here.']