【问题标题】:Python Selenium Multiple buttons with same classPython Selenium 具有相同类的多个按钮
【发布时间】:2018-03-09 20:50:25
【问题描述】:

我正在尝试借助 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 中启动演示和真实账户???

【问题讨论】:

  • 您希望这些链接在两个新标签页中打开吗?
  • 是的,点击按钮会自动打开新标签
  • 编辑了答案
  • 报错:
  • 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")

标签: python selenium-chromedriver


【解决方案1】:

您可以通过 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

【讨论】:

  • 可以分享网站的链接吗?
  • 不幸的是,我不能,因为它与我的帐户详细信息有关。我通过直接在主页上发送用户名和密码密钥成功登录帐户,但现在我需要通过手动选择演示或真实来启动云平台。我希望你能明白
猜你喜欢
  • 2021-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-24
  • 2016-11-01
  • 2021-03-02
  • 1970-01-01
相关资源
最近更新 更多