小编Ria*_*ani的帖子

Python Selenium Webdriver如何在表中循环并打印出第2列的值

我有一个带有桌子的网页.该表在第二个td列的每一行中都有一些值.我打算输出的值是在div标签内的span类中.检查html树我可以看到值,例如"Name"在第1行(tr [1]),第2列(TD [2])例如html

<tr class="GAT4PNUFG GAT4PNUMG" __gwt_subrow="0" __gwt_row="0">
            <td class="GAT4PNUEG GAT4PNUGG GAT4PNUHG GAT4PNUNG">
            <td class="GAT4PNUEG GAT4PNUGG GAT4PNUNG">
                <div __gwt_cell="cell-gwt-uid-324" style="outline-style:none;">
                    <span class="linkhover" title="Name" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;color:#00A;cursor:pointer;">Name</span>
                </div>
            </td>
Run Code Online (Sandbox Code Playgroud)

我想遍历每一行的表并打印出第2列中的值,td [2]

我正在使用Python与Selenium Webdriver

表格第1列第2列的完整Xpath是:

html/body/div[2]/div[2]/div/div[4]/div/div[2]/div/div[3]/div/div[5]/div/div[3]/div/div[4]/div/div[2]/div/div[4]/div/div[3]/div/div[2]/div/div/table/tbody/tr[1]/td[2]/div/span
Run Code Online (Sandbox Code Playgroud)

我在想如果我可以从表开始,xpath如下:html/body/div [2]/div [2]/div/div [4]/div/div [2]/div/div [3]/DIV/DIV [5]/DIV/DIV [3]/DIV/DIV [4]/DIV/DIV [2]/DIV/DIV [4]/DIV/DIV [3]/DIV/DIV [2] /格/ DIV /台/ TBODY

然后我可以使用for循环并使用tr和td的索引,例如row1使用tr [i],col2使用td [2].

html/body/div[2]/div[2]/div/div[4]/div/div[2]/div/div[3]/div/div[5]/div/div[3]/div/div[4]/div/div[2]/div/div[4]/div/div[3]/div/div[2]/div/div/table/tbody
Run Code Online (Sandbox Code Playgroud)

如何循环遍历此表并打印出始终位于表的第2列的Span类标记的值?

我试图将表的开头变为变量,然后我可以使用它来循环遍历行和列.我需要一些帮助.

html/body/div[2]/div[2]/div/div[4]/div/div[2]/div/div[3]/div/div[5]/div/div[3]/div/div[4]/div/div[2]/div/div[4]/div/div[3]/div/div[2]/div/div/table/tbody/tr[i]/td[2]/div/span
Run Code Online (Sandbox Code Playgroud)

我忘了粘贴HTML.它是:

table = self.driver.find_element(By.XPATH, 'html/body/div[2]/div[2]/div/div[4]/div/div[2]/div/div[3]/div/div[5]/div/div[3]/div/div[4]/div/div[2]/div/div[4]/div/div[3]/div/div[2]/div/div/table/tbody')
Run Code Online (Sandbox Code Playgroud)

python selenium html-table selenium-webdriver

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

Python Selenium Exception AttributeError:"'service'对象在selenium.webdriver.ie.service.Service中没有属性'process'"

我有一个Selenium Python测试套件.它开始运行但几分钟后抛出以下错误:

Exception AttributeError: "'Service' object has no attribute 'process'" in <bound method Service.__del__ of <selenium.webdriver.ie.service.Service object at 0x0000000002610DD8>> ignored
Run Code Online (Sandbox Code Playgroud)

我的测试套件实现是:

import unittest
from HTMLTestRunner2 import HTMLTestRunner
import os
import Regression_TestCase.RegressionProject_TestCase2


# get the directory path to output report file
#result_dir = os.getcwd()
result_dir = r"E:\test_runners\selenium_regression_test_5_1_1\ClearCore - Regression Test\TestReport"

# get all tests from SearchProductTest and HomePageTest class
search_tests = unittest.TestLoader().loadTestsFromTestCase(Regression_TestCase.RegressionProject_TestCase2.RegressionProject_TestCase2)

# create a test suite combining search_test
re_tests = unittest.TestSuite([search_tests])

# open the report file
outfile = open(result_dir + …
Run Code Online (Sandbox Code Playgroud)

python selenium python-2.7 selenium-webdriver

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

我如何模拟慢速网络连接

可能重复:
模拟慢速网络连接的网络工具

我有一个投注服务器,它将投注数据发送到游戏终端我想模拟慢速网络连接.我希望网络真的很忙,负载很重,所以我可以看到下注服务器如何执行,以及当网络负载很重时它会超时.

我可以使用任何工具来做到这一点吗?

谢谢你的帮助.此致,Riaz

performance networking simulate

7
推荐指数
1
解决办法
6045
查看次数

按索引从下拉列表中选择第一项不起作用。未绑定的方法 select_by_index

我正在尝试从下拉列表中单击第一个项目。

我想使用它的索引值,因为该值每次都可能不同。

对于这个特定的测试,我只需要在下拉列表中选择第一个项目。

我试过 Select.select_by_index(1)

我收到错误:

    Traceback (most recent call last):
  File "C:\Webdriver\ClearCore 501 Regression Test\ClearCore - Regression Test\TestCases\DataPreviewsPage_TestCase.py", line 398, in test_a2_sort_data_preview_advanced
    data_previews_view_page.select_option_from_new_sort_drop_down() # Select the sort from the sort drop down to view the sorted fields
  File "C:\Webdriver\ClearCore 501 Regression Test\ClearCore - Regression Test\Pages\data_previews_view.py", line 144, in select_option_from_new_sort_drop_down
    Select.select_by_index(1) # select the 1st item from the sort drop down
TypeError: unbound method select_by_index() must be called with Select instance as first argument (got int instance instead)
Run Code Online (Sandbox Code Playgroud)

我调用下拉菜单的代码片段是:

def …
Run Code Online (Sandbox Code Playgroud)

python selenium python-2.7

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

Python文件名,不是标记.打开此文件并将文件句柄传递给Beautiful Soup

我已经更改了我的Python 2.7例程以接受文件路径作为例程的参数,因此我不必通过在方法内插入多个文件路径来复制代码.

当我的方法被调用时,我收到以下错误:

looks like a filename, not markup. You should probably open this file and pass the filehandle into Beautiful Soup.
  '"%s" looks like a filename, not markup. You should probably open this file and pass the filehandle into Beautiful Soup.' % markup)
Run Code Online (Sandbox Code Playgroud)

我的方法实现是:

def extract_data_from_report3(filename):
    html_report_part1 = open(filename,'r').read()
    soup = BeautifulSoup(filename, "html.parser")
    th = soup.find_all('th')
    td = soup.find_all('td')

    headers = [header.get_text(strip=True) for header in soup.find_all("th")]
    rows = [dict(zip(headers, [td.get_text(strip=True) for td in row.find_all("td")]))
        for row in soup.find_all("tr")[1:-1]]
    print(rows) …
Run Code Online (Sandbox Code Playgroud)

beautifulsoup python-2.7

7
推荐指数
2
解决办法
5946
查看次数

Selenium Python等待文本出现在元素错误显示中需要3个参数2

我正在使用WebdriverWait等待一些文本出现在网页上的元素中.我正在使用Selenium和Python.我的语法不正确.我收到错误:TypeError:init()需要3个参数(给定2个):

错误跟踪:

Traceback (most recent call last):
  File "E:\test_runners 2 edit project\selenium_regression_test_5_1_1\Regression_TestCase\RegressionProjectEdit_TestCase.py", line 2714, in test_000057_run_clean_and_match_process
    process_lists_page.wait_for_run_process_to_finish()
  File "E:\test_runners 2 edit project\selenium_regression_test_5_1_1\Pages\operations.py", line 334, in wait_for_run_process_to_finish
    EC.text_to_be_present_in_element("No data to display"))
TypeError: __init__() takes exactly 3 arguments (2 given)
Run Code Online (Sandbox Code Playgroud)

我的代码片段是:

def wait_for_run_process_to_finish(self): # When the process is running use WebdriverWait to check until the process has finished.  No data to display is shown when process has completed.
    try:
        WebDriverWait(self.driver, 900).until(
            EC.text_to_be_present_in_element("No data to display"))
        no_data_to_display_element = self.get_element(By.ID, 'operations_monitoring_tab_current_ct_fields_no_data')
        print …
Run Code Online (Sandbox Code Playgroud)

python selenium selenium-webdriver

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

Python ValueError:关闭文件的I/O操作.示例教程不起作用

我正在学习如何阅读和写入文件的教程.我收到以下错误.我不懂为什么.

C:\Python27\python.exe "C:/Automation/Python/Write to files/test3.py"
Traceback (most recent call last):
  File "C:/Automation/Python/Write to files/test3.py", line 8, in <module>
    f.read('newfile.txt', 'r')
ValueError: I/O operation on closed file
Run Code Online (Sandbox Code Playgroud)

我的代码是

f = open("newfile.txt", "w")
f.write("hello world\n")
f.write("Another line\n")
f.close()

f.read('newfile.txt', 'r')
print f.read()
Run Code Online (Sandbox Code Playgroud)

我试图放在f.close代码的底部,但我仍然得到相同的错误.

如果我注释掉,写部分可以工作f.read.它失败了f.read.

python

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

Selenium Webdriver Python页面对象MainPageLocators类为什么在类名的前面使用星号

我一直在使用Python进行Selenium Webdriver测试的页面对象模型。我遵循了GitHub上的示例。网址是:https : //github.com/baijum/selenium-python/blob/master/source/page-objects.rst

当您从MainPageLocators类(例如,从URL)中调用定位器时。

element = self.driver.find_element(*MainPageLocators.GO_BUTTON)
Run Code Online (Sandbox Code Playgroud)

它在类名* MainPageLocators前面使用星号。为什么使用*?

如果使用MainPageLocators,则不起作用,必须使用* MainPageLocators。

这不好,因为当我使用WebDriverWait时,它不适用于* MainPageLocators或MainPageLocators。例如

element = WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((*MainPageLocators.locator)))
Run Code Online (Sandbox Code Playgroud)

我必须以这种方式进行操作才能正常工作,这使将定位器放在一个地方的目的无法实现。

element = WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.ID, 'button_id')))
Run Code Online (Sandbox Code Playgroud)

为什么在MainPageLocators前面加星号?为什么* MainPageLocators在WebDriverWait内部不起作用?如果你做的话就可以了

self.driver.find_element(* MainPageLocators.locator)

但是,如果在WebDriverWait中使用它,它将不起作用

谢谢,里亚兹

selenium python-2.7 selenium-webdriver

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

WebdriverWait显示TimeoutException,如果我使用sleep.time它可以正常工作

我想在Python Webdriver中单击元素时使用WebdriverWait.使用WebdriverWait时出现以下TimeoutException错误:

Traceback (most recent call last):
  File "C:\Users\riaz.ladhani\PycharmProjects\Selenium Webdriver\ClearCore\TestCases\AdministrationPage_TestCase.py", line 30, in test_add_Project
    administration_page = login_page.clickAdministration()
  File "C:\Users\riaz.ladhani\PycharmProjects\Selenium Webdriver\ClearCore\Pages\login.py", line 46, in clickAdministration
    WebDriverWait (self.driver, 10).until(lambda d: self.driver.find_element(*MainPageLocators.AdministrationButton_xpath).click())
  File "C:\Python27\lib\site-packages\selenium\webdriver\support\wait.py", line 75, in until
    raise TimeoutException(message, screen, stacktrace)
TimeoutException: Message: 
Run Code Online (Sandbox Code Playgroud)

如果我使用time.sleep(10)它工作正常并单击元素.我暂时将所有链接恢复到time.sleep,直到我可以WebdriverWait正常工作.

我的WebdriverWait代码片段是:

class LoginPage(BasePage):

    #Click Administration from top menu
    def clickAdministration(self):
        WebDriverWait (self.driver, 10).until(lambda d: self.driver.find_element(*MainPageLocators.AdministrationButton_xpath).click())
        #time.sleep(10)
        return AdministrationPage(self.driver)
Run Code Online (Sandbox Code Playgroud)

进口是:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException


class LoginPage_TestCase(unittest.TestCase):

     def test_add_Project(self):
        login_page = login.LoginPage(self.driver) …
Run Code Online (Sandbox Code Playgroud)

python selenium webdriver selenium-webdriver

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

在我的日期时间值中,我想使用正则表达式从时间中去除斜杠和冒号并用下划线替换它

我正在使用Python,Webdriver进行自动化测试.我的方案是在我们网站的管理页面上,我单击添加项目按钮,然后输入项目名称.

我输入的项目名称格式LADEMO_IE_05/20/1515:11:38 为最后的日期和时间.

我想要做的是使用正则表达式我想找到/和:并用下划线替换它们_

我已经计算出正则表达式:

[0-9]{2}[/][0-9]{2}[/][0-9]{4}:[0-9]{2}[:][0-9]{2}
Run Code Online (Sandbox Code Playgroud)

这找到2位数,然后/是2位数,然后/依此类推.

我想更换/:使用_.

我可以使用import re在Python中执行此操作吗?我需要一些语法方面的帮助.

我返回日期的方法是:

def get_datetime_now(self):
            dateTime_now = datetime.datetime.now().strftime("%x%X")
            print dateTime_now #prints e.g. 05/20/1515:11:38
            return dateTime_now
Run Code Online (Sandbox Code Playgroud)

我在文本字段中输入项目名称的代码片段是:

project_name_textfield.send_keys('LADEMO_IE_' + self.get_datetime_now())
Run Code Online (Sandbox Code Playgroud)

输出是例如

LADEMO_IE_05/20/1515:11:38
Run Code Online (Sandbox Code Playgroud)

我希望输出为:

LADEMO_IE_05_20_1515_11_38 
Run Code Online (Sandbox Code Playgroud)

python regex selenium webdriver selenium-webdriver

4
推荐指数
2
解决办法
1173
查看次数