Python Selenium:更改表页面时如何单击表的内容?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (194)
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
from bs4 import BeautifulSoup
import time

url = "https://www.bungol.ca/"
driver = webdriver.Firefox(executable_path ='/usr/local/bin/geckodriver')
driver.get(url)

#Select toronto by default
driver.find_element_by_xpath("""/html/body/section/div[2]/div/div[1]/form/div/select/optgroup[1]/option[1]""").click()
time.sleep(1)
driver.find_element_by_xpath("""/html/body/section/div[2]/div/div[1]/form/div/button""").click()
driver.find_element_by_xpath("""/html/body/nav/div[1]/ul[1]/li[3]/select/option[8]""").click()
#select last 2 years
driver.find_element_by_xpath("""//*[@id="activeListings"]""").click()


#opening sold listing in that area
driver.find_element_by_xpath("""/html/body/div[5]/i""").click() #closes property type slide
driver.find_element_by_xpath("""//*[@id="navbarDropdown"]""").click()
driver.find_element_by_xpath("""//*[@id="listViewToggle"]""").click()


def data_collector():
    hidden_next = driver.find_element_by_class_name("nextPaginate")
    #inputs in textbox
    inputElement = driver.find_element_by_id('navbarSearchAddressInput')
    inputElement.send_keys('M3B2B6')
    time.sleep(1)
    #inputElement.send_keys(Keys.ENTER)
    row_count = 3
    table = driver.find_elements_by_css_selector("""#listViewTableBody""")

    while hidden_next.is_displayed(): #while there is a next page button to be pressed

        time.sleep(3) #delay for table refresh

        #row_count = len(driver.find_elements_by_css_selector("""html body#body div#listView.table-responsive table#listViewTable.table.table-hover.mb-0 tbody#listViewTableBody tr.mb-2"""))           
        for row in range(row_count): #loop through the rows found

            #alternate row by changing the tr index
            driver.find_element_by_xpath("""/html/body/div[8]/table/tbody/tr[""" + str(row + 1) + """]/td[1]""").click()
            time.sleep(2)
            print(driver.find_element_by_css_selector("""#listingStatus""").text) #sold price

            #closes the pop up after getting the data
            driver.find_element_by_css_selector('.modal-xl > div:nth-child(1) > div:nth-child(1) > button:nth-child(1)').click()
            time.sleep(1)

        #clicks next page button for the table    
        driver.find_element_by_xpath("""//*[@id="listViewNextPaginate"]""").click()    


if __name__ == "__main__":
    data_collector()

代码循环遍历第一个表中的所有行(当前设置为3用于测试),每行单击 - 弹出窗口显示,抓取信息并关闭弹出窗口。 但是当它点击下一页时,它不会点击第二页的任何行。 它也没有显示找不到行xpath的错误。 但是显示弹出窗口关闭按钮的错误,因为弹出窗口没有打开,因为没有按下行来显示弹出窗口。

当表格翻转到下一页时,如何使其单击行?

提问于
用户回答回答于

我的理解是,你的脚本遍历列表,打开列表,获取列表状态,关闭列表,并对所有列表执行相同的操作。

如果我的理解是正确的,下面的代码可能对你有帮助:

from selenium.webdriver import Firefox
from selenium.webdriver.support.select import Select
import time

driver = Firefox(executable_path=r'path to geckodriver.exe')
driver.get('https://www.bungol.ca/') 
driver.maximize_window()
driver.implicitly_wait(10)

# Select toronto by default
driver.find_element_by_css_selector('#locationChoice button[type="submit"]').click()

sold_in_the_last = Select(driver.find_element_by_id('soldInTheLast'))
sold_in_the_last.select_by_visible_text('2 Years')

driver.find_element_by_id('activeListings').click()

# opening sold listing in that area
driver.find_element_by_css_selector('#leftSidebarClose>i').click()
driver.find_element_by_id('navbarDropdown').click()
driver.find_element_by_id('listViewToggle').click()

def get_listings():
    listings_table = driver.find_element_by_id('listViewTableBody')
    listings_table_rows = listings_table.find_elements_by_tag_name('tr')
    return listings_table_rows

def get_sold_price(listing):
    listing.find_element_by_css_selector('td:nth-child(1)').click()
    time.sleep(2)
    sold_price = driver.find_element_by_id('listingStatus').text

    time.sleep(2)
    close = driver.find_elements_by_css_selector('.modal-content>.modal-body>button[class="close"]')
    close[2].click()
    time.sleep(2)

    return sold_price

def data_collector():
    data = []
    time.sleep(2)
    next = driver.find_element_by_id('listViewNextPaginate')

    # get all the listing prior to the last page
    while next.is_displayed():
        listings = get_listings()
        for listing in listings:
            data.append(get_sold_price(listing))

        next.click()

    # get listings from last page
    listings = get_listings()
    for listing in listings:
        data.append(get_sold_price(listing))

    return data

if __name__ == '__main__':
    from pprint import pprint
    data = data_collector()
    pprint(data)
    print(len(data))
用户回答回答于

在浏览器中,当我单击第二页中的行时,也无法打开弹出。所以我认为这可能是网站的错。

如果要检查元素是否存在,可以使用以下代码:

def check_exists_by_xpath(xpath, driver):
    try:
        driver.find_element_by_xpath(xpath)
    except NoSuchElementException:
        return False
    return True

扫码关注云+社区

领取腾讯云代金券