这篇文章主要围绕SeleniumWeb驱动程序:findElement和By.name…和无头浏览器展开,旨在为您提供一份详细的参考资料。我们将全面介绍SeleniumWeb驱动程序:findElem
这篇文章主要围绕Selenium Web驱动程序:findElement和By.name…和无头浏览器展开,旨在为您提供一份详细的参考资料。我们将全面介绍Selenium Web驱动程序:findElement的优缺点,解答By.name…和无头浏览器的相关问题,同时也会为您带来Python Selenium find_element_by_name、Python Selenium Web驱动程序:send_keys在无头模式下不起作用、python – WebElement上的Selenium WebDriver“find_element_by_xpath”、Ruby Selenium Web驱动程序:如何计算特定节点的子元素节点的实用方法。
本文目录一览:- Selenium Web驱动程序:findElement(By.name…和无头浏览器(selenium edge无头)
- Python Selenium find_element_by_name
- Python Selenium Web驱动程序:send_keys在无头模式下不起作用
- python – WebElement上的Selenium WebDriver“find_element_by_xpath”
- Ruby Selenium Web驱动程序:如何计算特定节点的子元素节点
Selenium Web驱动程序:findElement(By.name…和无头浏览器(selenium edge无头)
我正在尝试遵循Selenium Webdrive教程
http://www.toolsqa.com/selenium-webdriver/headless-browser-testing-selenium-
webdriver/
有一个简单的测试,这里是步骤:
开启网页http://google.com
获取页面标题。
搜索“ Selenium”
再次检查页面标题。
从类代码示例开始,这是我的代码
package headlessBrowser;import org.openqa.selenium.By;import org.openqa.selenium.WebElement;import org.openqa.selenium.htmlunit.HtmlUnitDriver;public class TestOne {public static void main(String[] args) { // Declaring and initialising the HtmlUnitWebDriver HtmlUnitDriver unitDriver = new HtmlUnitDriver(); // open google.com webpage unitDriver.get("http://google.com"); System.out.println("Title of the page is -> " + unitDriver.getTitle()); // find the search edit box on the google page WebElement searchBox = unitDriver.findElement(By.name("q")); // type in Selenium searchBox.sendKeys("Selenium"); // find the search button WebElement button = unitDriver.findElement(By.name("gbqfba")); // Click the button button.click(); System.out.println("Title of the page is -> " + unitDriver.getTitle()); }}
尝试执行它,我遇到以下错误
Title of the page is -> Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element with name: q
没有打印页面名称:????? 似乎在页面中找不到“ q”元素。????
我检查了Firebug,似乎代码中存在“ q”元素(在下面的代码片段中查找name =“ q” …)
<input spellcheck="false" dir="ltr"aria-autocomplete="both" role="combobox" aria-haspopup="false"id="lst-ib" maxlength="2048" name="q" autocomplete="off" title="Cerca" value="" aria-label="Cerca" type="text">
我在Windows 7上使用Eclipse Luna
有什么建议?先感谢您 …
切萨雷
答案1
小编典典我已经解决了....我在组织中的代理后面,所以我必须设置代理。
我发现了这一点:HtmlUnitDriver似乎没有正在加载page。
寻找FunThomas424242评论,并观看此链接https://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/htmlunit/HtmlUnitDriver.html
因此,正确的代码如下:
package headlessBrowser;import org.openqa.selenium.By;import org.openqa.selenium.WebElement;import org.openqa.selenium.htmlunit.HtmlUnitDriver;public class TestOne {public static void main(String[] args) { // Declaring and initialising the HtmlUnitWebDriver HtmlUnitDriver unitDriver = new HtmlUnitDriver(); // Necessary set Proxy if you''re behind it !!!! unitDriver.setProxy("proxy.YOUR-ORGANIZATION.COM", XXXX); // open google.com webpage unitDriver.get("http://www.google.com"); System.out.println("Title of the page is -> " + unitDriver.getTitle()); // find the search edit box on the google page WebElement searchBox = unitDriver.findElement(By.name("q")); // type in Selenium searchBox.sendKeys("Selenium"); // find the search button WebElement button = unitDriver.findElement(By.name("btnG")); // Click the button button.click(); System.out.println("Title of the page is -> " + unitDriver.getTitle()); }}
“核心”行如下
// Necessary set Proxy if you''re behind it !!!! unitDriver.setProxy("proxy.YOUR-ORGANIZATION.COM", XXXX);
您必须在其中更新代理配置的位置。
Python Selenium find_element_by_name
如何解决Python Selenium find_element_by_name?
试试这个:
elem=browser.find_element_by_name("Email")
find_element_by_name
期望name
HTML行中的的值。所以在这种情况下name="Email"
,你会给find_element_by_name("Email")
。
解决方法
什么是正确的代码来查找电子邮件输入 https://accounts.google.com/ServiceLogin?
的HTML是 <input id="Email"type="email" spellcheck="false" value=""
placeholder="Email" name="Email"></input>
我引用的是https://pypi.python.org/pypi/selenium
from selenium import webdriver
browser = webdriver.Firefox()
browser.get(''https://accounts.google.com/ServiceLogin?'')
因为elem = browser.find_element_by_name()
我已经尝试过:
elem=browser.find_element_by_name(''input id="Email"'')
elem=browser.find_element_by_name(''input id="Email"type="email" spellcheck="false" value="" placeholder="Email" name="Email"'')
elem=browser.find_element_by_name(''input id="Email"'')
elem=browser.find_element_by_name(''id="Email"type="email" spellcheck="false" value="" placeholder="Email" name="Email"'')
elem=browser.find_element_by_name(''id="Email"'')
这些都不起作用。
Python Selenium Web驱动程序:send_keys在无头模式下不起作用
在选项中尝试添加此参数
chrome_options.add_argument("user-agent = my-user-agent")
my-user-agent
对于每个系统都是不同的,请检查您的代理商Google my user agent
并将其插入选项中。
与此同时,我觉得在您的代码中,while
是不必要的,您可以删除它。
由于您尝试访问的文本框需要花费一些时间才能加载,因此请在提取文本框时尝试在此处使用显式等待
WebDriverWait(driver,20).until(EC.presence_of_element_located((By.CSS_SELECTOR,"#identifierId")))
当您必须输入文本并按Enter时,请使用以下代码段更新代码:
username = WebDriverWait(driver,"#identifierId")))
username.send_keys("Emailuser",Keys.ENTER)
python – WebElement上的Selenium WebDriver“find_element_by_xpath”
我正在尝试使用以下行查找元素:
elements = driver.find_elements_by_xpath("//div[@]")
一旦我有了元素,我知道有两个“显示”,我希望能够使用第二个,并在其中找到一个特定的元素,如下所示:
title = elements[1].find_element_by_xpath("//div[@]")
但是,它总是恢复使用第一个.我已经介入了它,它正在为“显示”找到2个元素,所以我不确定我做错了什么.
任何帮助将不胜感激.
elements = driver.find_elements_by_xpath("//div[@]")
title = elements[1].find_elements_by_xpath(".//div[@]")
Ruby Selenium Web驱动程序:如何计算特定节点的子元素节点
是初学者的selenium程序员,请帮助我…
我有下面的HTML,并试图计算元素的子节点 <div>
为4(1 div
和3个li
元素)
<div>
<div>
<li><a><button>ADD ATTACHMENTS</button></a></li><input type="file" multiple="">
</div>
<li><a href="javascript:void(0)"><button>SIGN</button></a></li>
<li><a><button>DRAFT</button></a></li>
<li> <a href="javascript:void(0)"><svgviewBox="0 0 100 100"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#trash"></use></svg> </a></li>
</div>
目前正在按以下方式分别计算计数(row
变量在html上方)
li_actions_count = row.find_elements(:xpath => "./li").length
div_actions_count = row.find_elements(:xpath => "./div").length
有人可以使用seleniumWeb驱动程序在Ruby中用一种简单的方法来帮助我吗
我们今天的关于Selenium Web驱动程序:findElement和By.name…和无头浏览器的分享已经告一段落,感谢您的关注,如果您想了解更多关于Python Selenium find_element_by_name、Python Selenium Web驱动程序:send_keys在无头模式下不起作用、python – WebElement上的Selenium WebDriver“find_element_by_xpath”、Ruby Selenium Web驱动程序:如何计算特定节点的子元素节点的相关信息,请在本站查询。
本文标签: