这是代码......
from selenium import webdriver
url = 'https://infobypokharelk.blogspot.com/'
driver = webdriver.Firefox()
driver.get(url)
row_count = len(driver.find_elements_by_xpath("//*[@id='post-body-6767393087210111064']/div[1]/table/tbody/tr"))
col_count = len(driver.find_elements_by_xpath("//*[@id='post-body-6767393087210111064']/div[1]/table/tbody/tr[1]/td"))
print("Number if Rows:",row_count)
print("Number of Columns",col_count)
first_part = "//*[@id='post-body-6767393087210111064']/div[1]/table/tbody/tr["
secound_part = "]/td["
third_part = "]"
for n in range(1,row_count+1):
for m in range(1,col_count+1):
final_path = first_part + str(n) + secound_part + str(m) + third_part
table_data = driver.find_elements_by_xpath(final_path).text
print(table_data,end = " ")
print()
Run Code Online (Sandbox Code Playgroud)
输出是..
File "tut_td.py", line 15, in <module>
table_data = driver.find_elements_by_xpath(final_path).text
AttributeError: 'list' object has no attribute 'text'
Run Code Online (Sandbox Code Playgroud) 下面这段代码的完整含义是什么?我的意思是,哪个对象实现哪个类并使用哪个方法?
driver.getPageSource().contains("Text to find");
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 selenium 读取来自https://web.telegram.org的电报消息。
当我在 firefox 中打开https://web.telegram.org时,我已经登录,但是当从 selenium webdriver(firefox) 打开同一页面时,我得到登录页面。
我看到 telegram web 不使用 cookie 进行身份验证,而是将值保存在本地存储中。我可以使用selenium访问本地存储并在那里拥有密钥,例如:“dc2_auth_key”,“dc2_server_salt”,“dc4_auth_key”,...但我不知道如何处理它们才能登录(如果我这样做需要对它们做一些事情,那么为什么?它是同一个浏览器,为什么在没有硒的情况下打开时它不能正常工作?)
重现:
打开 Firefox 并登录https://web.telegram.org然后运行以下代码:
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("https://web.telegram.org")
# my code is here but is irrelevant since im at the login page.
driver.close()
Run Code Online (Sandbox Code Playgroud) python selenium webdriver selenium-webdriver firefox-profile
收到以下块的 AttributeError: 'str' 对象没有属性 'text' 错误:
`version = driver.find_element_by_id('com.project.PROJECT:id/version').text
print(version)
for i in version:
if 'Version : 1.2.0.133' == str(i):
print('Step 46. NAS version is displayed correctly - PASS')
else:
print('Step 46. NAS version is incorrect - Fail')
time.sleep(2)
pass`
Run Code Online (Sandbox Code Playgroud)
也尝试过: if 'Version : 1.2.0.133' == i.text
还是行不通。
print(version)
返回正确的值:版本:1.2.0.133
但我无法打印if value is true: print('Step 46. NAS version is displayed correctly - PASS')
正在向我发送垃圾邮件else print FAIL value
另外,如果我使用.text
for EC 等待也会返回错误。
谢谢
我在尝试使用 Python 中的 Selenium 从网页打印 PDF 时遇到问题。有问题的网页是 https://jamabandi.nic.in/land%20records/NakalRecord。我尝试从每个下拉列表中选择第一条记录,然后单击“Nakal”按钮生成 PDF。
但是,即使网页上存在表格,生成的 PDF 始终为空。我尝试过手动打印到 PDF 操作和使用 Selenium 自动打印,但在这两种情况下,生成的 PDF 都是空的。
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
service = Service()
options = webdriver.ChromeOptions()
# Set up preferences for printing to PDF
settings = {
"recentDestinations": [{"id": "Save as PDF", "origin": "local", "account": ""}],
"selectedDestinationId": "Save as PDF",
"version": 2
}
prefs = {
'printing.print_preview_sticky_settings.appState': json.dumps(settings),
'printing.print_to_file': True,
'printing.print_to_file.path': '/Users/jatin/Downloads/output.pdf' # Specify the desired output path
}
chrome_options.add_experimental_option('prefs', prefs)
import urllib.request …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Page Object模式创建WebDriver UI测试框架,使用以下URL作为参考:http://www.peternewhook.com/2010/09/automated-testing-pageobjects-webdriver/
根据例子,我创建了3个类(见下文).问题在于return PageFactory.InitElements(_driver, page);
SearchPage类的Search方法中的行.
当我尝试构建时,我收到以下错误:
"OpenQA.Selenium.ISearchContext"类型在未引用的程序集中定义.您必须添加对程序集'WebDriver的引用
很公平,因为我正在引用WebDriver.Common.dll,所以我尝试删除它并将WebDriver.dll添加到我的引用中,突然间我在构建时得到以下内容:
无法将类型'void'隐式转换为'ConsoleApplication1.ResultsPage'
它失败了; 当我将鼠标悬停在它上面时,它说:
无法将表达式类型'void'转换为'ConsoleApplication1.ResultsPage'.
我也尝试引用这两个程序集,并认为我可以使用不同的使用,但它是一个禁止,不起作用.
使用WebDriver.dll时为什么不能返回PageFactory.InitElements?
有没有办法绕过它,或者我可以通过略微改变架构来实现相同的结果?
非常感谢您的帮助.谢谢.
using OpenQA.Selenium;
namespace ConsoleApplication1
{
public class Page
{
public IWebDriver _driver;
public Page(IWebDriver driver)
{
this._driver = driver;
}
}
}
using OpenQA.Selenium;
namespace ConsoleApplication1
{
public class ResultsPage : Page
{
public ResultsPage(IWebDriver driver)
: base(driver)
{
}
private IWebElement count;
public string GetPagesReturned()
{
return count.Text;
}
}
}
using OpenQA.Selenium;
using OpenQA.Selenium.Support.PageObjects;
namespace ConsoleApplication1 …
Run Code Online (Sandbox Code Playgroud) ChromeDriver开发团队已经解决了这个问题:http://code.google.com/p/selenium/issues/detail? id = 1725&q = chrome%20file%20upload&solpec = ID%20Stars%20Type%20Status%20Priority%20Milestone %20Owner%20Summary
但我无法实现它.有人可以向我展示一个示例或使用ChromeDriver在Chrome上测试文件上传的解决方法吗?
我的系统中安装了chrome.我正在使用Selenium对chrome进行一些测试.
我已将Chromedriver.exe下载到MyDocuments.我使用System.setProperty()设置了'webdriver.chrome.driver'并启动了ChromeDriver().它工作正常.
System.setProperty("webdriver.chrome.driver", "C:\\MyDocuments\\chromedriver.exe");
driver=new ChromeDriver();
Run Code Online (Sandbox Code Playgroud)
现在,我正在尝试将Chromedriver.exe放入远程计算机"https://remotemachine/chromedriver.exe"中.当我设置System属性并启动ChromeDriver()时,我得到一个异常,Selenium在一个奇怪的路径中搜索chrome:
webdriver.chrome.driver定义的chromedriver可执行文件在C:\ Users ..\Appdata\Local\Google Chrome\Application ...\https://remotemachine/chromedriver.exe中不存在
为什么Selenium通过将系统属性附加到C盘中的某个位置来搜索chromedriver.exe?
如何使用远程chromedriver.exe文件从Selenium启动Chrome?
与上述无关,但是:
是否也可以使用Java/Selenium找到默认的浏览器二进制路径?
如何仅选择父跨度webelement而不是孩子?
列表summaryLinks = summary.findElements(By.xpath(""));
我的HTML代码:
<li>
<div class="mod-indent mod-indent-2">
<a href="http://mysite.com">
<img class="activityicon" alt="URL" src="http://mysite.com">
<span class="instancename">LinkText <span class="accesshide "> URL</span></span>
</a>
</div>
</li>
Run Code Online (Sandbox Code Playgroud)
我尝试了下面的xpath但是没有用
.//div [@ class ='mod-indent mod-indent-2']/a/span [@ class ='instancename']
.//div [@class='mod-indent mod-indent-2']/a/span [1]
.//div [@ class ='mod-indent mod-indent-2']/a/span /.
.//div [@class='mod-indent mod-indent-2']/a/span [position()= 1]
.//div [@class='mod-indent mod-indent-2']/a/span/span [position()= 1]
我在Eclipse中使用selenium web-driver.我需要检查表是否显示在页面上.我用这样的代码:
try {
Assert.assertTrue(driver.findElement(By.xpath(".//*[@id='flexibleTable']")).isDisplayed());
} catch (AssertionError e) {
System.err.println("overview not found: " + e.getMessage());
}
Run Code Online (Sandbox Code Playgroud)
也阻止if
if (driver.findElement(By.xpath(".//*[@id='flexibleTable']")).isDisplayed()) {
...
} else {
...
}
Run Code Online (Sandbox Code Playgroud)
但如果找不到这样的元素,测试就会中断.如何组织检查以便我的测试继续,即使元素不在页面上,其他块将执行?
编辑 失败跟踪
org.openqa.selenium.NoSuchElementException: Unable to find element with xpath == .//*[@id='flexibleTable'] (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 360 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.23.1', revision: '17143', time: '2012-06-08 18:59:04'
System info: os.name: 'Windows XP', os.arch: 'x86', os.version: '5.1', …
Run Code Online (Sandbox Code Playgroud) webdriver ×10
selenium ×6
java ×4
python ×3
android ×1
attributes ×1
list ×1
pandas ×1
python-3.x ×1
web-scraping ×1
xpath ×1