如何用显式等待替换这种隐式等待?
驱动程序 = 新的 ChromeDriver(功能);
driver.manage().deleteAllCookies();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
这在 Before 方法中使用。我能够替换代码中的所有 Thread.sleep(),但我不确定对这个做了什么。
我正在尝试使用Selenium webdriver在Python中构建一个web爬虫,但是当我从webdriver检索网站源代码时,无法访问所需的信息。
我认为问题在于,一旦最初从服务器下载了页面,就会通过JavaScript将内容添加到页面。运行时browser.page_source,在添加此内容之前,我先获取页面的源代码。我想知道添加JavaScript加载的内容后是否可以获得页面的源代码(换句话说,当我使用Inspect Element查看页面时看到的内容)。
这是我正在使用的基本Python脚本:
from selenium import webdriver
browser = webdriver.Chrome()
browser.get("https://www.opportunities.auckland.ac.nz")
print(browser.page_source)
Run Code Online (Sandbox Code Playgroud)
当我运行上面的脚本时,我获得了在浏览器中查看页面源代码时看到的页面源代码(即,当使用inspect元素查看代码时,看不到其他内容)。
我尝试过的事情
time.sleep(10)在我访问源代码时,如果页面没有完全加载,请在各个位置添加。get_attribute("innerHTML")在身体上使用。execute_script()让JS运行。execute_script()来使JS脚本一个接一个地运行。如果有人可以首先告诉我这是否可行,以及是否将我指出正确的方向,那将是很好的。谢谢。
更新1
尝试Piotrek的解决方案时,我得到以下输出:
Warning (from warnings module):
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/phantomjs/webdriver.py", line 49
warnings.warn('Selenium support for PhantomJS has been deprecated, please use headless '
UserWarning: Selenium support for PhantomJS has been deprecated, please use headless versions of Chrome or Firefox instead
<html><head></head><body></body></html>
Run Code Online (Sandbox Code Playgroud)
不幸的是,这似乎行不通。
我知道它们都可以用来验证一个元素是否出现在页面上,但我想知道每个元素的性能影响和可读性影响。所以我的问题是,假设您有 20 个元素要检查页面,
或者
selenium robotframework selenium-webdriver selenium2library webdriverwait
有没有办法使用 PageFactory 注释等待 Selenium 中不存在的元素?
使用时:
@FindBy(css= '#loading-content')
WebElement pleaseWait;
Run Code Online (Sandbox Code Playgroud)
定位元素,然后:
wait.until(ExpectedConditions.invisibilityOfElementLocated(pleaseWait));
Run Code Online (Sandbox Code Playgroud)
我会得到:
org.opeqa.selenium.WebElement cannot be converted to org.openqa.selenium.By
Run Code Online (Sandbox Code Playgroud)
我可以使用以下方法完成我需要的操作:
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector('loading-content')));
Run Code Online (Sandbox Code Playgroud)
但是,我希望能够使用 PageFactory 注释以保持框架一致。有没有办法做到这一点?
我正在尝试更改表单输入中的输入字段值,但无法这样做。
输入值位于此URL
Email: Vela.admi@gmail.com
PW: Testing@123
在这些操作之后页面上会出现表单:
1. on left side click on "Sell Order" red button in Dashboard widget
2. a popup will appear on click, select "Order Type" as "Limit"
3. a new formfield appears, named "Limit Price"
Run Code Online (Sandbox Code Playgroud)
当我尝试使用 selenium 或 JS 更改它时,当我们按“发送订单”提交表单时,它似乎没有更改
我的代码路径是:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
import time
driver.get(url)
driver.find_element_by_css_selector('#gwt-uid-770 > span > span > input').send_keys('6700')
Run Code Online (Sandbox Code Playgroud)
我也试过
driver.execute_script("document.querySelector('#gwt-uid-770 > span > span > …Run Code Online (Sandbox Code Playgroud) javascript selenium selenium-chromedriver selenium-webdriver webdriverwait
我试图用这个 XPATH 在 selenium 中找到一个元素。我在 Firefox 网络浏览器中得到了这个。
/html/body/div[5]/div[2]/div[9]/div[1]/div[2]/div/div[2]/div[2]/div/div/div[1]/div[2]/div[1]/a
Run Code Online (Sandbox Code Playgroud)
<a href="/url?sa=t&rct=j&q=&esrc=s&source=web&cd=&cad=rja&uact=8&ved=2ahUKEwiw7cbBv6LqAhVMAHIKHbbFCUYQFjAAegQIBxAB&url=https%3A%2F%2Fwww.google.com%2Fgmail%2F&usg=AOvVaw3mZ_qbD_gQyp_sqkjrwStn" onmousedown="return rwt(this,'','','','','AOvVaw3mZ_qbD_gQyp_sqkjrwStn','','2ahUKEwiw7cbBv6LqAhVMAHIKHbbFCUYQFjAAegQIBxAB','','',event)" data-ctbtn="2" data-cthref="/url?sa=t&rct=j&q=&esrc=s&source=web&cd=&cad=rja&uact=8&ved=2ahUKEwiw7cbBv6LqAhVMAHIKHbbFCUYQFjAAegQIBxAB&url=https%3A%2F%2Fwww.google.com%2Fgmail%2F&usg=AOvVaw3mZ_qbD_gQyp_sqkjrwStn"><br><h3 class="LC20lb DKV0Md">Gmail by Google</h3><div class="TbwUpd NJjxre"><cite class="iUh30 bc tjvcx">www.google.com<span class="eipWBe"> › gmail</span></cite></div></a>
Run Code Online (Sandbox Code Playgroud)
我的硒代码
driver.findElement(By.xpath("/html/body/div[5]/div[2]/div[9]/div[1]/div[2]/div/div[2]/div[2]/div/div/div[1]/div[2]/div[1]/a")).click();
driver.findElement(By.linkText("Sign in")).click();
Run Code Online (Sandbox Code Playgroud)
但它不起作用。帮我。
我可以在很长一段时间内使用Selenium Web Driver吗?
即使我可以像下面那样设置隐式等待命令,它也不会等到我给出的时间.
driver.manage().timeouts().implicitlyWait(5, TimeUnit.MINUTES);
Run Code Online (Sandbox Code Playgroud)
这里有什么不对吗?
在我的情况下,我需要执行一个测试用例并等待4分钟,然后执行下一个测试用例.
我在这里使用Java.
selenium dom selenium-webdriver webdriverwait expected-condition
我试图使用 selenium 从 google chrome 下载文件。我下面使用的代码运行良好。但不知怎的,它不再起作用了。有任何想法吗?
import os.path
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import Select
RAWDATA_URL = 'https://oui.doleta.gov/unemploy/DataDownloads.asp'
options = webdriver.ChromeOptions()
prefs = {'download.default_directory' : SAVE_PATH, "download.prompt_for_download": False}
options.add_experimental_option('prefs', prefs)
driver = webdriver.Chrome(executable_path = DRIVE_PATH, chrome_options = options)
driver.get(RAWDATA_URL)
time.sleep(5)
Run Code Online (Sandbox Code Playgroud)
driver.find_element_by_xpath("//*[@id='main']/table[38]/tbody/tr[2]/td[5]/a").click()
Run Code Online (Sandbox Code Playgroud)
driver.get("https://oui.doleta.gov/unemploy/csv/ar9047.csv")
Run Code Online (Sandbox Code Playgroud)
我预计 csv 文件可以成功下载。但谷歌浏览器只是告诉我“失败-下载错误”。
import datetime
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 python 中的 Selenium 单击网站上的按钮,并且单击按钮工作正常,我可以看到按钮被单击,但代码此时停止运行,当我在普通浏览器中检查网站时,我可以告诉按钮还没有被点击。更具体地说,我试图单击服务器start的按钮aternos.org,并且单击效果很好,但由于某种原因,服务器的实际启动并未完成。
我的开始按钮点击代码:
start = driver.find_element(By.ID, 'start')
status = driver.find_element(By.CLASS_NAME, 'statuslabel-label').text
print(status)
if status == 'Offline':
start.click()
print('Starting server!')
Run Code Online (Sandbox Code Playgroud) selenium ×10
webdriverwait ×10
java ×3
python ×3
xpath ×3
javascript ×2
dom ×1
iframe ×1
implicitwait ×1
page-factory ×1
pageobjects ×1
qa ×1
webdriver ×1