标签: webdriverwait

使用 Selenium 使用 WindowHandles 跟踪和迭代选项卡和窗口的最佳方法

我们正在与 Selenium webdriver 合作,对 Internet Explorer 11 进行 UI 测试。\xc2\xa0\n在测试的 Web 应用程序中,会弹出几个屏幕。在一些测试中,我们最终得到了三个浏览器窗口,因此也得到了三个 Driver.WindowHandles。\xc2\xa0\n要从一个 WindowHandle 切换到另一个,我们预计 Driver.WindowHandles 将按照最旧的窗口在前、最新的窗口在后的方式进行排序。但事实并非如此:它完全是随机的!\xc2\xa0

\n\n

因为窗口句柄是一个 GUID,所以我们最终创建了一个字典,以 WindowHandle GUID 作为键,其值为浏览器窗口中加载的页面类型。\n但这也会导致在关闭窗口时维护字典。\ xc2\xa0

\n\n

对于这么简单的事情来说,这似乎是一个很大的工作量。对此有更好的解决方案吗?

\n

c# selenium window-handles selenium-webdriver webdriverwait

5
推荐指数
1
解决办法
4319
查看次数

等到可见和等到位于 Selenium 之间有什么区别

我正在使用wait(until.elementLocated(element, timeout))wait(until.elementVisible(element, timeout))。“等待直到可见”在“等待直到定位”失败的地方失败了。为什么?

selenium webdriver selenium-webdriver webdriverwait expected-condition

5
推荐指数
1
解决办法
5627
查看次数

消息:尝试通过 Selenium 单击下拉菜单中的选项时,元素 <option> 无法滚动到视图中

我正在尝试选择一个下拉菜单并选择一个选项。我正在使用最新版本的 Selenium、最新版本的 Firefox、最新版本的 geckodriver 和最新版本的 Python。

这是我的问题:当我尝试选择一个选项时,出现以下错误:

selenium.common.exceptions.ElementNotInteractableException: Message: Element <option> could not be scrolled into view.
Run Code Online (Sandbox Code Playgroud)

我尝试了各种方法来解决这个问题,但似乎都不起作用。以下是我尝试过的一些方法。

mySelectElement = browser.find_element_by_id('providerTypeDropDown')
dropDownMenu = Select(mySelectElement)
dropDownMenu.select_by_visible_text('Professional')

mySelectElement = browser.find_element_by_id('providerTypeDropDown')
dropDown = Select(mySelectElement)
for option in dropDown.options:
    message = option.get_attribute('innerText')
    print(message)
    if message == 'Professional':
        print("Exists")
        dropDown.select_by_visible_text(message) 
        break

element = browser.find_element_by_id('providerTypeDropDown')
browser.execute_script("var select = arguments[0]; for(var i = 0; i < select.options.length; i++){ if(select.options[i].text == arguments[1]){ select.options[i].selected = true; } }", element, "Professional")
Run Code Online (Sandbox Code Playgroud)

HTML 代码遵循通常的选择标签和选项标签。任何帮助表示赞赏。HTML 代码包含在下面。

<select data-av-chosen="providerTypes" id="providerTypeDropDown" data-placeholder="Please Select a …
Run Code Online (Sandbox Code Playgroud)

python selenium selenium-webdriver drop-down-menu webdriverwait

5
推荐指数
1
解决办法
3万
查看次数

通过 Python3 使用 Selenium 和 WebDriver 切换选项卡时出现“NoSuchWindowException:没有这样的窗口:窗口已关闭”

我有一个表单,当我单击它时会在新选项卡中打开。当我尝试导航到该新选项卡时,我不断收到 NoSuchWindowException。代码非常简单。“myframe”是新选项卡中的框架,信息最终将插入其中。我应该等待其他事情吗?

from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait, Select
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException, NoSuchElementException
import time
import pandas as pd

url = *****
driver = webdriver.Chrome(executable_path = r'S:\Engineering\Jake\MasterControl Completing Pipette CalPM Forms\chromedriver')
driver.get(url)

wait = WebDriverWait(driver, 5)    

window_before = driver.window_handles[0]
driver.find_element_by_id('portal.scheduling.prepopulate').click()
window_after = driver.window_handles[1]
driver.switch_to_window(window_after)
driver.switch_to_default_content()
wait.until(EC.frame_to_be_available_and_switch_to_it('myframe'))
Run Code Online (Sandbox Code Playgroud)
Traceback (most recent call last):

  File "<ipython-input-308-2aa72eeedd51>", line 1, in <module>
    runfile('S:/Engineering/Jake/MasterControl Completing Pipette CalPM Forms/Pipette Completing CalPM Tasks.py', wdir='S:/Engineering/Jake/MasterControl Completing …
Run Code Online (Sandbox Code Playgroud)

python selenium webdriver selenium-webdriver webdriverwait

5
推荐指数
1
解决办法
3万
查看次数

如何在Python中使用react.js和Selenium从网页中抓取数据?

我在抓取一个使用的网站时遇到一些困难react.js,但不确定为什么会发生这种情况。

这是网站的 html: 在此输入图像描述

我想做的是单击带有 的按钮class: play-pause-button btn btn -naked。但是,当我使用 Mozilla gecko webdriver 加载页面时,会抛出异常

Message: Unable to locate element: .play-pause-button btn btn-naked
Run Code Online (Sandbox Code Playgroud)

这让我觉得也许我应该做点别的事情来获得这个元素?到目前为止,这是我的代码:

driver.get("https://drawittoknowit.com/course/neurological-system/anatomy/peripheral-nervous-system/1332/brachial-plexus---essentials")
    # execute script to scroll down the page
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);var lenOfPage=document.body.scrollHeight;return lenOfPage;")
    time.sleep(10)        
    soup = BeautifulSoup(driver.page_source, 'lxml')
    print(driver.page_source)
    play_button = driver.find_element_by_class_name("play-pause-button btn btn-naked").click()
    print(play_button)
Run Code Online (Sandbox Code Playgroud)

有谁知道我该如何解决这个问题?任何帮助深表感谢

python selenium web-scraping reactjs webdriverwait

5
推荐指数
1
解决办法
8969
查看次数

Selenium findElements() 是否必须隐式等待返回 0 个元素?

我带着一个关于 Selenium 的问题来到这里。在我的测试中,我需要删除网络应用程序中的某些项目,然后我想验证项目列表是否为空。我知道这看起来微不足道,但我有一些小问题。这就是我想检查我的项目列表是否为空的方式:

Assert.assertEquals(page.getSearchResultList().size(), 0);
Run Code Online (Sandbox Code Playgroud)

简单且有效,但...由于隐式等待而缓慢。

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Run Code Online (Sandbox Code Playgroud)

事实上,当我删除项目时,thengetSearchResultList().size()是 0 并且 Selenium 总是等待 10 秒,然后 findElements() 返回 0 大小。

为了避免这 10 秒的等待,我有一个解决方法,可以在断言之前修改隐式等待,但我认为这不是一个好主意。

page.getDriver().manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
Assert.assertEquals(page.getSearchResultList().size(), 0);
Run Code Online (Sandbox Code Playgroud)

还有其他更好的解决方案吗?

请求更新 @KunduK

不使用 WebDriverWait 进行断言:

    Instant start = Instant.now();
    List<WebElement> resultSearchList = page.getDriver().findElements(By.cssSelector("[ng-repeat='searchResult in $ctrl.searchResults']"));
    Assert.assertEquals(resultSearchList.size(), 0);
    Instant stop = Instant.now();
    log.debug("Assert Took: " + Duration.between(start, stop).getSeconds() + " sec");
Run Code Online (Sandbox Code Playgroud)

输出:

10:49:59.081 [main] DEBUG impl.AssertNewEntityPage - Assert Took: 10 sec
Run Code Online (Sandbox Code Playgroud)

使用 WebDriverWait 进行断言

    Instant start = Instant.now();
    new WebDriverWait(page.getDriver(), 10).until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector("[ng-repeat='searchResult in …
Run Code Online (Sandbox Code Playgroud)

java selenium webdriver webdriverwait implicitwait

5
推荐指数
1
解决办法
1714
查看次数

如何使用 xpath 中的 contains 查找 aria-label 元素

我正在尝试获取锚标记内的信息,但不获取href. 我想提取 eBay 上一些卖家的评分。在下面的 HTML 代码中,您可以看到评级分数的位置。有没有办法在不使用的情况下获取有关“Bewertungspunktestand”(德语评分)的信息href,因为href卖家之间的变化?本例中的评分为 32。由于文本“Bewertungspunktestand”仅在这一行中,我认为可以让它搜索该文本并提取包含该文本的 aria-label。

\n

这是此示例的链接

\n

这是我尝试过的 Python 代码,但没有成功:

\n
try: \n    trans = driver.find_element_by_xpath("//a[@aria-label=\'Bewertungspunktestand\']")\nexcept:\n    trans = \'0\'\n
Run Code Online (Sandbox Code Playgroud)\n

这是 HTML 代码

\n
<span class="mbg-l">\n    (<a href="http://feedback.ebay.de/ws/eBayISAPI.dll?ViewFeedback&amp;userid=thuanhtran&amp;iid=133585540546&amp;ssPageName=VIP:feedback&amp;ftab=FeedbackAsSeller&amp;rt=nc&amp;_trksid=p2047675.l2560" aria-label="Bewertungspunktestand: 32">32</a>\n    <span class="vi-mbgds3-bkImg  vi-mbgds3-fb10-49" aria-label="Gelber Stern f\xc3\xbcr 10 bis 49 Bewertungspunkte" role="img"></span>)\n</span>\n
Run Code Online (Sandbox Code Playgroud)\n

python selenium xpath css-selectors webdriverwait

5
推荐指数
2
解决办法
2万
查看次数

如何使用 Selenium ChromeDriver 和 Chrome 在自定义位置下载文件

我想将 txt 和 pdf 文件下载到特定文件夹。但它只是将它们下载到另一个文件夹中。网站http://demo.automationtesting.in/FileDownload.html。代码有问题还是我没有放置正确的文件夹位置?

import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chromeOptions=Options()
chromeOptions.add_experimental_option("prefs", {"download.default_dictionary": "C:\DownloadedAutomationFiles"})

driver=webdriver.Chrome(executable_path="D:\ChromeDriverExtracted\chromedriver", chrome_options=chromeOptions)
driver.get("http://demo.automationtesting.in/FileDownload.html")
driver.maximize_window()

driver.find_element_by_id("textbox").send_keys("testing")
driver.find_element_by_id("createTxt").click()   #generate file button
driver.find_element_by_id("link-to-download").click()    #dowload link


#Download PDF FILE

driver.find_element_by_id("pdfbox").send_keys("testing download text file")
driver.find_element_by_id("createPdf").click()   #generate file button
driver.find_element_by_id("pdf-link-to-download").click()    #dowload link

time.sleep(2)
driver.close()
Run Code Online (Sandbox Code Playgroud)

python selenium google-chrome selenium-chromedriver webdriverwait

5
推荐指数
1
解决办法
1万
查看次数

使用 Selenium Python 按 value 属性查找元素

我正在尝试在网页中查找特定元素。它必须是 value 中文本内的特定数字...在这种情况下,我需要通过数字 565657 查找元素(请参阅下面的外部 HTML)

我已经通过 xpath: 尝试过("//*[contains(text(),'565657')]"),但("//input[@value='565657']")它不起作用。

有人可以帮忙吗?提前致谢!

当您从网页上的元素复制时,原始 XPATH 只是“ //*[@id="checkbox15"]”,我需要找到一种方法来查找下面提到的数字。

python selenium xpath css-selectors webdriverwait

5
推荐指数
1
解决办法
6334
查看次数

如何使用 Selenium 单击 cloudflare 的“验证您是人类”复选框挑战

我需要使用 Python 自动下载此网页中的 .csv 文件:

https://pace.coe.int/en/aplist/committees/9/commission-des-questions-politiques-et-de-la-democratie
Run Code Online (Sandbox Code Playgroud)

现在,我写了这段代码:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
import time
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
 
chromedriver_path = r"./driver/chromedriver"
browser = webdriver.Chrome(executable_path=chromedriver_path)
url = "https://pace.coe.int/en/aplist/committees/9/commission-des-questions-politiques-et-de-la-democratie"
topics_xpath = '//*[@id="challenge-stage"]/div/label/span[2]'
browser.get(url)
time.sleep(5)  #Wait a little for page to load.
escolhe = browser.find_element("xpath", topics_xpath)
time.sleep(5)
escolhe.click()
time.sleep(5)
Run Code Online (Sandbox Code Playgroud)

网页打开,然后提示我单击“验证您是人类”:

在此输入图像描述

我已经“检查”了按钮并复制了 xpath(参见上面的代码)。但我收到这个错误:

NoSuchElementException: no such element: Unable to …
Run Code Online (Sandbox Code Playgroud)

iframe xpath cloudflare selenium-webdriver webdriverwait

5
推荐指数
2
解决办法
9741
查看次数