我有一个 Selenium 脚本(Python),它点击回复按钮使anonemail类出现。anonemail 类出现的时间各不相同。因此,我必须使用 sleep 直到元素出现。
我想等到课程出现而不是使用睡眠。我听说过等待命令,但我不知道如何使用它们。
这是我迄今为止所拥有的:
browser.find_element_by_css_selector(".reply-button").click()
sleep(5)
email=browser.find_element_by_css_selector(".anonemail").get_attribute("value")
Run Code Online (Sandbox Code Playgroud) python selenium selenium-webdriver webdriverwait expected-condition
我正在使用硒来抓取一些数据。
我点击的页面上有一个按钮说“custom_cols”。此按钮为我打开一个窗口,我可以在其中选择我的列。
这个新窗口有时需要一些时间才能打开(大约 5 秒)。所以为了处理这个我用过
WebDriverWait
Run Code Online (Sandbox Code Playgroud)
延迟为 20 秒。但有时它无法在新窗口中选择查找元素,即使该元素可见。这种情况只有十次发生一次,其余时间它都可以正常工作。
我也在其他地方使用了相同的功能(WebDriverWait),它按预期工作。我的意思是它会等到元素可见,然后在找到它的那一刻点击它。
我的问题是为什么即使我正在等待元素可见,新窗口上的元素也不可见。要在这里添加,我试图增加延迟时间,但我仍然偶尔会遇到该错误。
我的代码在这里
def wait_for_elem_xpath(self, delay = None, xpath = ""):
if delay is None:
delay = self.delay
try:
myElem = WebDriverWait(self.browser, delay).until(EC.presence_of_element_located((By.XPATH , xpath)))
except TimeoutException:
print ("xpath: Loading took too much time!")
return myElem
select_all_performance = '//*[@id="mks"]/body/div[7]/div[2]/div/div/div/div/div[2]/div/div[2]/div[2]/div/div[1]/div[1]/section/header/div'
self.wait_for_elem_xpath(xpath = select_all_performance).click()
Run Code Online (Sandbox Code Playgroud) python selenium web-scraping webdriverwait expected-condition
你能举例说明差异之间text_to_be_present_in_element并
text_to_be_present_in_element_value用一个例子,最好在Python?
以下链接似乎解释了 text_to_be_present_in_element 的工作原理,但我仍然不清楚。
http://www.seleniumframework.com/python-basic/waits-and-synchronization/
我正在尝试使用 Python 来抓取一个网站,该网站通过使用嵌入的 javascript 文件将数据作为响应呈现到 HTML 中来动态加载它的 HTML。因此,如果我单独使用 BeautifulSoup,我将无法检索我需要的数据,因为我的程序会在 Javascript 加载数据之前抓取它。因此,我将 selenium 库集成到我的代码中,以使我的程序在抓取网站之前等待找到某个元素。
我最初是这样做的:
element = WebDriverWait(driver,100).until(EC.presence_of_element_located((By.ID, "tabla_evolucion")))
Run Code Online (Sandbox Code Playgroud)
但我想通过执行以下操作来指定一个类:
element = WebDriverWait(driver,100).until(EC.presence_of_element_located((By.class, "ng-binding ng-scope")))
Run Code Online (Sandbox Code Playgroud)
这是我的其余代码:
driver_path = 'C:/webDrivers/chromedriver.exe'
driver = webdriver.Chrome(executable_path=driver_path)
driver.header_overrides = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36'}
url = "myurlthatIamscraping.com"
response = driver.get(url)
html = driver.page_source
characters = len(html)
element = WebDriverWait(driver,100).until(EC.presence_of_element_located((By.class, "ng-binding ng-scope")))
print(html)
print(characters)
time.sleep(10)
driver.quit()
Run Code Online (Sandbox Code Playgroud)
它对我不起作用,我在任何地方都找不到正确的语法。
python selenium selenium-webdriver webdriverwait expected-condition
Traceback (most recent call last):
File "Inventorytest.py", line 88, in <module>
j.go_to_application()
File "Inventorytest.py", line 65, in go_to_application
EC.element_to_be_clickable((By.ID, 'FavoriteApp_ITEM'))
File "/home/naroladev/Mercury_Back-End/mercuryenv/lib/python3.6/site-packages/selenium/webdriver/support/wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
Run Code Online (Sandbox Code Playgroud)
我在 EC2 服务器实例上遇到了上述异常。我的脚本在 Ubuntu 和 Mac 操作系统以及本地系统上任何版本的 firefox 和 geckodriver 上都可以正常工作。但是 EC2 ubuntu 18.04.01 版本出现上述错误,在此我也尝试升级和降级 firefox 和 geckodriver 版本,但仍然无法正常工作。谁能帮我提供建议和解决方案。
python selenium timeoutexception webdriverwait expected-condition
我正在使用wait(until.elementLocated(element, timeout))和wait(until.elementVisible(element, timeout))。“等待直到可见”在“等待直到定位”失败的地方失败了。为什么?
selenium webdriver selenium-webdriver webdriverwait expected-condition
selenium dom selenium-webdriver webdriverwait expected-condition
构建如何使用 Selenium for Python 等待页面加载?我正在尝试创建一种方法,允许使用预期条件轮询多个元素是否存在。
我收到错误“bool”对象在包含 wait.until(any(expectations)) 的行上不可调用。
思考过程是允许多个 Xpath 按预期条件传递,然后使用any(),以与此基于 java 的答案类似的方式,尝试使用 selenium xpath 等待页面中的两个元素之一,检查是否任何一个条件都存在。
在这种情况下使用any()的正确方法是什么?或者更好的是,需要做什么才能使该方法发挥作用?
假设 Selenium .get('url') 已在调用此方法之前立即执行。
def wait_with_xpath_expectation(self, search_elements, timeout=6, max_attempts=3):
"""
Selenium wait for an element(s) designated by Xpath to become available in the DOM. Useful for javascript AJAXy
loading pages where content may be be rendered dynamically on the client side after page load appears complete.
search_elements may be one Xpath as a string or many …Run Code Online (Sandbox Code Playgroud) 我在间歇性单击某个元素时遇到 TimeoutExceptions。我尝试过显式等待和 time.sleep()。它工作了一段时间,我一次又一次地遇到例外。
我想了解这是否是由预期条件引起的。
WebDriverWait(self.driver, 40).until(EC.element_to_be_clickable((By.XPATH, <locator> ))).click()
Run Code Online (Sandbox Code Playgroud)
如果我使用以下条件是否有助于避免超时异常?
element = WebDriverWait(self.driver, 40).until(EC.presence_of_element_located((By.XPATH, <locator> )))
element.click()
Run Code Online (Sandbox Code Playgroud) 我正在尝试单击此日历并使用 selenium 自动插入日期,但出现以下错误:
无效元素状态:元素必须是用户可编辑的才能清除它。
HTML 片段
<a id="enddate-dropdown" class="dropdown-toggle" role="button" data-toggle="dropdown" data-target="">
<p class="custom-datepickers__date-prefix ng-binding">To:</p>
<!-- ngIf: displayEndDate -->
<!-- ngIf: !displayEndDate --><div ng-if="!displayEndDate" class="custom-datepickers__no-date ng-scope"></div><!-- end ngIf: !displayEndDate -->
</a>
Run Code Online (Sandbox Code Playgroud)
代码片段
myclass.SetDateByXpath("//*[@id=\"enddate-dropdown\"]/p", myclass.GetDate("yyyy/MM/dd", mydate));
public void SetDateByXpath(String element, String value)
{
WebElement webElement = ExplicitWaitOnElement(By.xpath(element));
((JavascriptExecutor) driver).executeScript(
"arguments[0].removeAttribute('readonly','readonly')",webElement);
webElement.clear();
webElement.sendKeys(value);
}
Run Code Online (Sandbox Code Playgroud)
如果我手动设置日期,这是 HTML:
<a id="enddate-dropdown" class="dropdown-toggle" role="button" data-toggle="dropdown" data-target="">
<p class="custom-datepickers__date-prefix ng-binding">To:</p>
<!-- ngIf: displayEndDate --><p ng-if="displayEndDate" class="ng-binding ng-scope">2019/11/21</p><!-- end ngIf: displayEndDate -->
<!-- ngIf: !displayEndDate -->
</a>
Run Code Online (Sandbox Code Playgroud)
可能网站已更改,但现在我不知道如何设置此值。任何帮助将不胜感激。 …
selenium selenium-webdriver angular webdriverwait expected-condition
我正在构建一个网络抓取工具,它循环访问地址列表并在房地产网站上搜索它们。然后,它会根据我们已经了解的有关房产的信息更新一些下拉列表,然后再抓取各种信息,例如预期租金收益率。
搜索完每个地址后,可能需要一些时间才能将所需元素(例如“bathrooms_dropdown”)加载到网站上。我一直在使用它来管理它,time.sleep(x)但这很慢且不可靠,并且implictly_wait(60)似乎没有任何效果,因为我仍然经常收到“元素不存在/无法找到”错误。
我确信我需要实现 WebDriverWait,但在将其实现到我的代码中时无法计算出语法。我还没有看到任何与此结合使用的例子driver.find_elements()
任何帮助将不胜感激!
driver.get(url)
driver.implicitly_wait(60)
# find search bar and search for address
searchBar = driver.find_element(by = By.ID, value = 'react-select-3-input')
searchBar.send_keys(address)
searchButton = driver.find_element(By.CLASS_NAME, value='sc-1mx0n6y-0').click()
# wait for elements to load
time.sleep(3) # REPLACE THIS
# find dropdown and click to open it
bathrooms_dropdown = driver.find_elements(By.CLASS_NAME, value = 'css-19bqh2r')[-2]
bathrooms_dropdown.click()
Run Code Online (Sandbox Code Playgroud) python selenium selenium-webdriver webdriverwait expected-condition
wait = (driver, 10)
wait.until(EC.presence_of_element_located((By.XPATH, '//td[@class="blah blah blah"]')))
wait.until(EC.visibility_of_element_located((By.XPATH, '//h1[text() = "yo yo"]')))
Run Code Online (Sandbox Code Playgroud)
有没有一种方法可以将这两个条件组合成一行,或者以任何方式表明如果这两个条件都为真,那么在 Selenium、Python 中只有 click() 。