【问题标题】:selenium & python - cant interact with web elementselenium & python - 无法与 web 元素交互
【发布时间】:2021-10-27 02:16:26
【问题描述】:

正如我的标题所述,我收到一个错误,我的 selenium 给我一个错误,说 web 元素不可交互。基本上,我在圣克拉拉县税务包裹网站 (https://payments.sccgov.org/propertytax/Secured) 上并尝试使用硒来提取土地价值和税额的税单。我有一个包含所有税 APN 编号的 for 循环,应该循环通过每一个来拉取相关信息。去年我使用了相同的代码,一切都很好。出于某种原因,今年相同的代码不再有效。我猜他们实现了一些阻止 selenium 与 web 元素交互的东西。

我尝试使用等待计时器和 time.sleep 来确保 Web 元素加载,但这不是问题。如果我在查找之前使用 .wait 加载元素,则代码将永远等待直到超时。

感谢任何帮助!以下是我的代码:

for num, county in zip(lstOfParcelNums, lstofCounties):
    # get Santa Clara's tax website
    if "Santa Clara" in lstofCounties[count]:
        site.get('https://payments.sccgov.org/propertytax/Secured')

        time.sleep(5)

        #Inputting parcel number in search
        apn = site.find_element_by_xpath('//*[@id="ParcelNumber"]')
        apn.send_keys(num)
        go = site.find_element_by_xpath('//*[@id="ParcelSubmit"]')
        go.click()

        baldue1 = site.find_element_by_xpath('//*[@id="select_invoiceslist"]/div[1]/div[3]/div[2]/div[8]').text
        baldue2 = site.find_element_by_xpath('//*[@id="select_invoiceslist"]/div[1]/div[3]/div[4]/div[8]').text
        
        landval = site.find_element_by_id('billdetailstablemain').text

        #Populating into excel
        #Data
        fetchedData.cell(count+2,7,value = baldue1)
        fetchedData.cell(count+2,8,value = baldue2)

        wb.save("C:/Users/Custom Gaming PC/Desktop/Python/Projects/2019-20 Tax APN Output.xlsx")
        count+=1

【问题讨论】:

    标签: python selenium


    【解决方案1】:

    您是否尝试过使用完整的 Xpath 选项?这很有效,尽管它对页面中的任何更改都很敏感。

    apn = driver.find_elements_by_xpath('/html/body/div[3]/div/div[2]/div[1]/div[3]/div/div/form/div/div/div/div[1]/input')
    

    【讨论】:

    • 嗨,肖恩!谢谢你,它工作得很好!请问,您是如何找到完整的 XPATH 的?我通常检查元素,然后单击复制“xpath”,但没有看到完整 xpath 的选项。非常感谢您的帮助!
    • 嗨!谢谢,我很高兴这个答案对你有用。我添加了如何获得完整 xpath 的屏幕截图,这可能比解释更容易;但是...打开开发工具菜单,然后使用该功能单击一个项目以检查它或右键单击->再次检查网页中的对象将突出显示您的对象 int eh Elements 窗格。从那里您可以右键单击元素窗格中的元素并选择 COPY->Copy Full XPath。
    猜你喜欢
    • 2022-07-01
    • 1970-01-01
    • 2020-04-05
    • 2020-03-08
    • 2021-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-15
    相关资源
    最近更新 更多