使用Selenium获取页面中的文本、输入框、下拉框及其对应的坐标,可以通过以下步骤编写代码:\n\n1. 导入必要的库和模块\npython\nfrom selenium import webdriver\nfrom selenium.webdriver.common.action_chains import ActionChains\n\n\n2. 初始化WebDriver,打开浏览器\npython\ndriver = webdriver.Chrome() # 初始化Chrome浏览器\ndriver.get("https://example.com") # 打开目标网页\n\n\n3. 获取页面中的所有文本和元素\npython\n# 获取页面中的文本\nall_text = driver.find_element_by_tag_name('body').text\n\n# 获取页面中的所有输入框和下拉框\ninputs = driver.find_elements_by_tag_name('input')\nselects = driver.find_elements_by_tag_name('select')\n\n\n4. 获取文本、输入框和下拉框的坐标\npython\n# 获取文本的坐标\ntext_location = driver.find_element_by_tag_name('body').location\n\n# 获取输入框和下拉框的坐标\ninput_locations = [input.location for input in inputs]\nselect_locations = [select.location for select in selects]\n\n\n完整的代码如下:\npython\nfrom selenium import webdriver\nfrom selenium.webdriver.common.action_chains import ActionChains\n\ndriver = webdriver.Chrome()\ndriver.get("https://example.com")\n\n# 获取页面中的文本\nall_text = driver.find_element_by_tag_name('body').text\n\n# 获取页面中的所有输入框和下拉框\ninputs = driver.find_elements_by_tag_name('input')\nselects = driver.find_elements_by_tag_name('select')\n\n# 获取文本的坐标\ntext_location = driver.find_element_by_tag_name('body').location\n\n# 获取输入框和下拉框的坐标\ninput_locations = [input.location for input in inputs]\nselect_locations = [select.location for select in selects]\n\nprint("页面中的文本:", all_text)\nprint("输入框的坐标:", input_locations)\nprint("下拉框的坐标:", select_locations)\n\ndriver.quit()\n\n\n请注意,这段代码仅适用于获取页面中的文本、输入框和下拉框的坐标,对于其他类型的元素可能需要进行相应的修改。另外,为了获取准确的坐标,可能需要在代码中加入等待时间或使用显式等待。

Selenium 获取页面文本、输入框、下拉框及坐标 - 代码示例

原文地址: https://www.cveoy.top/t/topic/p9wg 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录