2016-12-26 58 views
0

任何人都可以通過示例向我建議如何使用selenium webdriver和Python處理國際化。如何通過在Python中使用Selenium WebDriver來處理國際化?

發現這篇文章,但它是與Java

https://nileshdk.wordpress.com/2013/08/06/internationalization-automating-localized-ui-using-selenium-webdriver/

尋找使用Web驅動程序+ Python的組合方式。

+0

該文章有大約5行非常簡單的代碼片段,它不依賴於語言 - 它在Python中看起來也完全一樣。只需根據當前的語言動態加載文本列表,您可以按照您喜歡的任何方式指定。 – skandigraun

+1

嘗試在'script.py'的頂部添加'# - * - coding:utf-8 - * - '。這應該允許你使用'「utf-8」'編碼 – Andersson

回答

0

爲國際實施例下面的代碼:

用於編輯:PyCharm

文件名:Internationalization.py

from selenium import webdriver 
from Day_3.csvReader import * 

for languages in test_execution(): 

    firefoxprofile = webdriver.FirefoxProfile() 
    firefoxprofile.set_preference("intl.accept_languages", languages) 

    driver = webdriver.Firefox(firefoxprofile) 
    driver.maximize_window() 
    driver.get("http://www.google.com") 

    text_leftbottom1 = ".//*[@id='fsl']/a[" 
    text_leftbottom2 = "]" 

    for i in range(1, 4): 
     for linktext in driver.find_elements_by_xpath(text_leftbottom1 + str(i) + text_leftbottom2): 
      print(linktext.text) 

    text_rightbottom1 = ".//*[@id='fsr']/a[" 
    text_rightbottom2 = "]" 

    for i in range(1, 3): 
     for linktext in driver.find_elements_by_xpath(text_rightbottom1 + str(i) + text_rightbottom2): 
      print(linktext.text) 

CSV文件:testsuite.csv

此文件包含名稱我們想要打開網站的語言。文件

結構:

Language 
pt-BR 
fr 

方法讀取csv文件:

導入CSV爲csv

def test_execution(): 
    with open('E:\\Study\\HackerRank_30DaysofCode\\Day_3\\testsuite.csv', 'r+') as file: 
     data = csv.DictReader(file) 
     result = [] 
     for row in data: 
      result.append((row['Language'])) 
     return result 

反饋非常歡迎!

相關問題