我想自动执行文件下载完成检查chromedriver
.
HTML
下载列表中的每个条目看起来像
<a is="action-link" id="file-link" tabindex="0" role="link" href="http://fileSource" class="">DownloadedFile#1</a>
Run Code Online (Sandbox Code Playgroud)
所以我使用以下代码来查找目标元素:
driver.get('chrome://downloads/') # This page should be available for everyone who use Chrome browser
driver.find_elements_by_tag_name('a')
Run Code Online (Sandbox Code Playgroud)
这将返回空列表,同时有3个新下载.
正如我发现的那样,只能#shadow-root (open)
处理标签的父元素.那么如何在这个#shadow-root
元素中找到元素呢?
我得到了与在线商店https://www.tiendasjumbo.co/buscar?q=mani相关的下一个网址,但我无法将产品标签提取到另一个字段:
from selenium import webdriver
import time
from random import randint
driver = webdriver.Firefox(executable_path= "C:\Program Files (x86)\geckodriver.exe")
driver.implicitly_wait(10)
time.sleep(4)
url = "https://www.tiendasjumbo.co/buscar?q=mani"
driver.maximize_window()
driver.get(url)
driver.find_element_by_xpath('//h1[@class="impulse-title"]')
Run Code Online (Sandbox Code Playgroud)
我做错了什么,我也尝试过切换 iframe 但没有办法实现我的目标?欢迎任何帮助。
什么是硒?
当您打开Selenium的官方页面时,您首先读到的是“什么是Selenium?”中的“ Selenium automates browser”。部分。“硒的哪个部分适合我?”部分 下面提供了Selenium WebDriver和Selenium IDE之间的选择。据此,我推断出Selenium是工具的集合,该集合包括IDE,WebDriver API(语言绑定),Grid,Selenium Standalone Server,浏览器驱动程序。必须下载适当的文件才能构建项目。
什么是WebDriver?
WebDriver是一个API。它用多种语言编写,这些语言称为语言绑定。API具有控制浏览器的功能。您可以使用这些功能编写脚本来以所需的方式(测试用例)控制浏览器。
这就是我所知道的。如果我错了,请纠正我。我想从面试的角度知道这两个问题的答案。
我一直在关注如何使用硒自动实现阴影DOM元素的讨论。与#shadow-root (open)
元素一起工作。
在通过“ 硒”访问url时出现Clear data的“ 清除浏览数据”弹出窗口中定位按钮的过程中,我无法找到以下元素:chrome://settings/clearBrowserData
#shadow-root (open)
<settings-privacy-page>
Run Code Online (Sandbox Code Playgroud)
快照:
使用Selenium,以下是我的代码试用以及遇到的相关错误:
尝试1:
WebElement root5 = shadow_root4.findElement(By.tagName("settings-privacy-page"));
Run Code Online (Sandbox Code Playgroud)
错误:
Exception in thread "main" org.openqa.selenium.JavascriptException: javascript error: b.getElementsByTagName is not a function
Run Code Online (Sandbox Code Playgroud)尝试2:
WebElement root5 = shadow_root4.findElement(By.cssSelector("settings-privacy-page"));
Run Code Online (Sandbox Code Playgroud)
错误:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"settings-privacy-page"}
Run Code Online (Sandbox Code Playgroud)尝试3:
WebElement root5 = (WebElement)((JavascriptExecutor)shadow_root4).executeScript("return document.getElementsByTagName('settings-privacy-page')[0]");
Run Code Online (Sandbox Code Playgroud)
错误:
Exception in thread "main" java.lang.ClassCastException: org.openqa.selenium.remote.RemoteWebElement cannot be cast to org.openqa.selenium.JavascriptExecutor …
Run Code Online (Sandbox Code Playgroud)我正在使用 Selenium (Python) 来自动化网页。我正在尝试从#shadow-root(用户代理)下的输入字段获取文本。我使用的Xpath:
driver.find_element_by_xpath("**//*/p-calendar/span/input**").text
Run Code Online (Sandbox Code Playgroud)
没有返回任何东西。附上我的 DOM 元素的屏幕截图。要求:从影子根获取文本:01:01
我想废弃 immowelt.de,但我无法通过 cookie 横幅。我尝试使用 sleep() 和 WebDriverWait 等待横幅加载,但是它们都不起作用。
这是网络驱动程序的代码
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
chrome_driver_path = '.../chromedriver'
url = 'https://www.immowelt.de/immobilienpreise'
driver = webdriver.Chrome(executable_path=chrome_driver_path)
driver.get(url)
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="uc-center-container"]/div[2]/div/div/div/div[1]/button'))).click()
driver.close()
Run Code Online (Sandbox Code Playgroud)
这是带有睡眠的代码
from selenium import webdriver
import time
from selenium.webdriver.common.by import By
chrome_driver_path = '.../chromedriver'
url = 'https://www.immowelt.de/immobilienpreise'
driver = webdriver.Chrome(executable_path=chrome_driver_path)
driver.get(url)
time.sleep(5)
driver.find_element(By.XPATH, '//*[@id="uc-center-container"]/div[2]/div/div/div/div[1]/button').click()
driver.close()
Run Code Online (Sandbox Code Playgroud) cookies selenium selenium-webdriver shadow-dom queryselector