当前位置:   article > 正文

Python Selenium 获取 属性 值_python selenium3.14 打印window的属性值

python selenium3.14 打印window的属性值

Windows 10 + Python 3.7 + selenium==3.141.0 + urllib3==1.26.2 + Google Chrome 120.0.6099.130 (64 位)

  1. import time
  2. from selenium import webdriver
  3. from selenium.webdriver.support.wait import WebDriverWait
  4. from selenium.webdriver.common.by import By
  5. if __name__ == '__main__':
  6. # 谷歌浏览器位置
  7. CHROME_PATH = r'C:\Program Files\Google\Chrome\Application\chrome.exe'
  8. # 谷歌浏览器驱动地址
  9. CHROMEDRIVER_PATH = (r'C:\Program Files\Google\Chrome\chromedriver-win32_120.0.6099.109\chromedriver-win32'
  10. r'\chromedriver.exe')
  11. options = webdriver.ChromeOptions()
  12. options.add_experimental_option("excludeSwitches", ["enable-automation"])
  13. options.add_experimental_option("useAutomationExtension", False)
  14. driver = webdriver.Chrome(executable_path=CHROMEDRIVER_PATH, options=options)
  15. options.binary_location = CHROME_PATH
  16. driver.get('https://www.baidu.com')
  17. print(driver.title)
  18. #
  19. wait = WebDriverWait(driver, 30, 0.5)
  20. # 获取属性的值 style
  21. element = wait.until(lambda diver: driver.find_element(By.ID, 'hotsearch_data'))
  22. style = element.get_attribute('style')
  23. print(type(style), style)
  24. # 获取输入框的值
  25. id = 'kw'
  26. element = wait.until(lambda diver: driver.find_element(By.ID, id))
  27. element.send_keys('你好')
  28. time.sleep(5)
  29. text = element.get_attribute('value')
  30. print(text)
  31. '''
  32. 运行结果:
  33. 百度一下,你就知道
  34. <class 'str'> display: none;
  35. 你好
  36. '''
'''
参考:
https://blog.csdn.net/weixin_45265081/article/details/121692135
'''
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/319166
推荐阅读
相关标签
  

闽ICP备14008679号