硒-每行中的意外行为查找元素

时间:2019-03-18 23:55:26

标签: java selenium selenium-chromedriver rows

硒,Chrome WebDriver,Java。

尝试使用以下代码循环引发行并得到意外行为。

 List<WebElement> divs = driver.findElementsByXPath("//div[@class = \"w-dyn-items\"][contains(.//*,'2019')]/div[@class = \"w-dyn-item\"][position() < 30]")
    for (WebElement div : divs) {
        DocumentWrapper doc = new DocumentWrapper()
        try {
            //grab document stuff
            doc.title = div.findElementByXPath("//h1").getText()
            log.info("\tTitle: ${doc.title}")
            doc.dateStr = div.findElementByXPath("//div[@class=\"meta-tag\"]").getText()
            log.info("\tDate: ${doc.dateStr}")
            String google_drive_link = div.findElementByXPath("//a[@class=\"drop-link\" and contains(. ,\"PDF Agenda\")]").getAttribute('href')
            log.info("\traw Url: ${google_drive_link}")

我得到的结果只是相同标题,日期和一遍又一遍的链接的重复

2019-03-18 16:47:39 [main] INFO ::  Title: City Council & Board/Commission Meetings
2019-03-18 16:47:39 [main] INFO ::  Date: DECEMBER 23, 2019
2019-03-18 16:47:39 [main] INFO ::  Url: https://drive.google.com/uc?export=download&id=1aAivoeNydSf761Hmih7lHWH7rLXkhuzV
2019-03-18 16:47:39 [main] INFO ::  Title: City Council & Board/Commission Meetings
2019-03-18 16:47:39 [main] INFO ::  Date: DECEMBER 23, 2019
2019-03-18 16:47:39 [main] INFO ::  raw Url: https://drive.google.com/open?id=1aAivoeNydSf761Hmih7lHWH7rLXkhuzV
2019-03-18 16:47:39 [main] INFO ::  Title: City Council & Board/Commission Meetings
2019-03-18 16:47:39 [main] INFO ::  Date: DECEMBER 23, 2019
2019-03-18 16:47:39 [main] INFO ::  Url: https://drive.google.com/uc?export=download&id=1aAivoeNydSf761Hmih7lHWH7rLXkhuzV
2019-03-18 16:47:39 [main] INFO ::  Title: City Council & Board/Commission Meetings
2019-03-18 16:47:39 [main] INFO ::  Date: DECEMBER 23, 2019
2019-03-18 16:47:39 [main] INFO ::  raw Url: https://drive.google.com/open?id=1aAivoeNydSf761Hmih7lHWH7rLXkhuzV
2019-03-18 16:47:39 [main] INFO ::  Title: City Council & Board/Commission Meetings
2019-03-18 16:47:39 [main] INFO ::  Date: DECEMBER 23, 2019
2019-03-18 16:47:39 [main] INFO ::  Url: https://drive.google.com/uc?export=download&id=1aAivoeNydSf761Hmih7lHWH7rLXkhuzV

现在,从技术上讲,这些不是表格行,而是一系列div。但是它们不应该一样工作吗?如果没有,谁可以向我解释为什么这不应该工作?

我要从https://www.cityofalhambra.org/city-meetings提取数据

1 个答案:

答案 0 :(得分:0)

好吧,我在github.io上阅读了Selenium文档,并找到了它。

enter image description here

它表示我们应该在XPath之前使用一个点作为相对路径。这是我最初的本能,但在以前的作品中,使用该点时出现了错误。将重试并更新我的答案。

更新*修复了!

Found document: 
    Title - City Council Public Hearing
    Date - MARCH 25, 2019
    Link - https://drive.google.com/uc?export=download&id=1aAivoeNydSf761Hmih7lHWH7rLXkhuzV
Found document: 
    Title - City Council Meeting (Regular)
    Date - MARCH 11, 2019
    Link - https://drive.google.com/uc?export=download&id=1bc1MBr_M10fJOVTkQTML7LFLzz_c28HB
Found document: 
    Title - City Council (Special)
    Date - MARCH 11, 2019
    Link - https://drive.google.com/uc?export=download&id=1odnxBz6oHps5C66mHCcijx2YS_y7jgM_
Found document: 
    Title - City Council Meeting (Regular) - Business Mtg. at 5:30 p.m. & Ceremonial at 7:00 p.m.
    Date - FEBRUARY 25, 2019
    Link - https://drive.google.com/uc?export=download&id=13NBhYiRDrw2egi6iRHLgI-BlRjYIr95N