ID正在变化时获取价值

时间:2018-02-19 13:24:53

标签: python windows python-3.x selenium selenium-webdriver

我需要检索值“[wkSu - '$ bS [U#;”来自HTML轰鸣声。问题是id =“ext-comp-1328”和id =“ext-gen1578”总是在变化。到目前为止我的代码到达特定页面:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys       
import time    

my_login="SuperMegaUser"
my_pin="960790"+input()

driver=webdriver.Chrome()
driver.get(r'https://cyberark.supermegacorp.com/PasswordVault/logon.aspx?ReturnUrl=%2fPasswordVault%2fdefault.aspx')
assert "Password Vault Sign In" in driver.title
driver.find_element_by_xpath(r'//*[@id="pvBody_PageTemplate_innerHolder_ctrlLogon_txtUsername"]').send_keys(my_login)
driver.find_element_by_xpath(r'//*[@id="pvBody_PageTemplate_innerHolder_ctrlLogon_txtPassword"]').send_keys(my_pin)
driver.find_element_by_xpath(r'//*[@id="pvBody_PageTemplate_innerHolder_ctrlLogon_btnLogon"]').click()
time.sleep(10)
#driver.find_element_by_xpath(r'//*[@id="ext-gen159"]/div/table/tbody/tr/td[19]').click()
driver.find_element_by_xpath(r'//*[@id="ext-gen159"]/div/table/tbody/tr/td[17]/div').click()
time.sleep(5)
driver.find_element_by_xpath(r'//*[@id="reason"]').send_keys("0")
driver.find_element_by_xpath(r'//*[@id="reason"]').send_keys(Keys.RETURN)
time.sleep(5)

HTML:

 <div class="password-windows-fieldset-body password-windows-fieldset-body-noheader" id="ext-gen1578"><label id="ext-comp-1327" class=" account-password-display password-labels">[wkSu-'$bS[U#;</label><table id="ext-comp-1328" cellspacing="0" class="x-btn password-window-copy-btn password-labels x-btn-noicon" style="width: auto;"><tbody class="x-btn-small x-btn-icon-small-left"><tr><td class="x-btn-tl"><i>&nbsp;</i></td><td class="x-btn-tc"></td><td class="x-btn-tr"><i>&nbsp;</i></td></tr><tr><td class="x-btn-ml"><i>&nbsp;</i></td><td class="x-btn-mc"><em class="" unselectable="on"><button type="button" id="ext-gen1593" class=" x-btn-text">Copy</button></em></td><td class="x-btn-mr"><i>&nbsp;</i></td></tr><tr><td class="x-btn-bl"><i>&nbsp;</i></td><td class="x-btn-bc"></td><td class="x-btn-br"><i>&nbsp;</i></td></tr></tbody></table></div>

2 个答案:

答案 0 :(得分:1)

要获取文字,请使用text代替click() text无括号

ext -

driver.find_element_by_xpath(r'//*[contains(@id,"ext-gen")]/div/table/tbody/tr/td[17]/div').click()

OR

driver.find_element_by_xpath(r'//*[matches(@id,"^.*?ext-gen.*$")]/div/table/tbody/tr/td[17]/div').click()

OR


driver.find_element_by_xpath(r'//*[matches(@id,"ext-gen")]/div/table/tbody/tr/td[17]/div').click()

ext-comp -

driver.find_element_by_xpath(r'//*[contains(@id,"ext-comp-")]/div/table/tbody/tr/td[17]/div').click()

OR


driver.find_element_by_xpath(r'//*[matches(@id,"^.*?ext-comp-.*$")]/div/table/tbody/tr/td[17]/div').click()

OR


driver.find_element_by_xpath(r'//*[matches(@id,"ext-comp-")]/div/table/tbody/tr/td[17]/div').click()

我发现了另外一个starts-with

driver.find_element_by_xpath(r'//*[starts-with(@id,"ext-gen")]/div/table/tbody/tr/td[17]/div').click()

driver.find_element_by_xpath(r'//*[starts-with(@id,"ext-comp-")]/div/table/tbody/tr/td[17]/div').click()

答案 1 :(得分:0)

鉴于您提供的HTML,下面的行应该可以满足您的需求。

driver.find_element_by_css_selector("label.account-password-display").text