Python Selenium具有相同类

时间:2017-09-28 07:39:50

标签: python selenium-chromedriver

我试图在python中使用selenium chromedriver进入平台。 但该平台提供演示和真实账户,提供多个具有相同类名的点击按钮。请参阅以下代码

<input type="hidden" name="username" value="username"> == $0
<input type="hidden" name="password" value="password"> == $0
<button type="submit" class="btn btn-launch" ng-transclude=""><button class="platform-btn ng-scope">Launch platform</button></button>
<button class="platform-btn ng-scope">Launch Platform</button>


<input type="hidden" name="username" value="username_demo"> == $0
<input type="hidden" name="password" value="password"> == $0
<button type="submit" class="btn btn-launch" ng-transclude=""><button class="platform-btn ng-scope">Launch platform</button></button>
<button class="platform-btn ng-scope">Launch Platform</button>

Xpath代码

真实

/html/body/ui-view/ui-view/div/div/div/div/div/ui-view/section/table/tbody/tr[1]/td[8]/tc-platform-launch/form/button/button

演示

/html/body/ui-view/ui-view/div/div/div/div/div/ui-view/section/table/tbody/tr[3]/td[8]/tc-platform-launch/form/button/button

如何在selenium python中启动演示和真实帐户?

1 个答案:

答案 0 :(得分:0)

你可以通过xpath直接找到元素。如果点击按钮打开一个新标签

,这将有效
browser=webdriver.Chrome()
real=browser.find_element_by_xpath("/html/body/ui-view/ui-view/div/div/div/div/div/ui-view/section/table/tbody/tr[1]/td[8]/tc-platform-launch/form/button/button")
demo=browser.find_element_by_xpath("/html/body/ui-view/ui-view/div/div/div/div/div/ui-view/section/table/tbody/tr[3]/td[8]/tc-platform-launch/form/button/button")
real.click()
demo.click()
browser.switch_to_window(browser.window_handles[-1])#To work on demo
browser.switch_to_window(browser.window_handles[-2])#to Work on real