python3 使用 selenium3 注意事项

    xiaoxiao2022-07-07  186

    selenium3 获取属性和元素中的文本内容

    1、通过 get_attribute(‘src’) 来获取属性的文本值
    rule = "//div[@class='access-check-code text-muted']//img" img_url = driver.find_element_by_xpath(rule).get_attribute('src')
    2、通过get_attribute(‘textContent’)来获取元素的文本值
    rule = "//div[@class='access-check-code text-muted']//img/div" img_infol = driver.find_element_by_xpath(rule).get_attribute('textContent')

    在使用 Headless Chrome 时遇到的问题

    options = Options() options.add_argument('--headless') driver = webdriver.Chrome(chrome_options=options)

    通过 chrome_options 选项添加 Chrome 参数时,会一直出现警告: DeprecationWarning: use options instead of chrome_options warnings.warn('use options instead of chrome_options', DeprecationWarning)“

    根据阅读源码的提示发现使用chrome_options 时会将 chrome_options 值传给options,然后在给一个警告信息,根据错误信息已经源码的注解了解到未来 options 会取代 chrome_options,所以我们只需要 chrome_options 改成 options 即可

    options = Options() options.add_argument('--headless') driver = webdriver.Chrome(chrome_options=options)
    最新回复(0)