脚本无法使用xpath或id或Selenium和Python

时间:2019-02-06 22:19:31

标签: python selenium xpath css-selectors webdriverwait

我在使用Python方面很陌生,但这似乎是我尝试编写的非常简单的脚本。我已经能够正确登录该网站,但是要进入下一步,我尝试单击显示“ Market Express”的按钮。

我能够看到xpath(// [@ id =“ MarketExpress”])以及按钮的ID(MarketExpress)。当我运行模块时,我收到以下错误消息:“无法找到元素:// [@ id =“ MarketExpress”]“

我甚至使用Firefox的插件“ xpath finder”仔细检查了xpath,以确保我具有正确的代码。

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains

usernameStr = '***'
passwordStr = '***'

driver = webdriver.Firefox()
driver.get(('https://www.myurl.com'))

username = driver.find_element_by_id('USERID')
username.send_keys(usernameStr)
password = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.ID, 'currentPassword')))
password.send_keys(passwordStr)
nextButton = driver.find_element_by_id('submit-button')
nextButton.click()

password = driver.find_element_by_name('currentPassword')
password.send_keys(passwordStr)
nextButton = driver.find_element_by_name('Submit')
nextButton.click()

marketExpress = driver.find_element_by_xpath('//*[@id="MarketExpress"]').click();

我尝试了许多不同的操作,但是无法使脚本单击此按钮,我将不胜感激!

下面是按钮所在的html:

<input class="crtordbtn" type="button" value="Market Express" `id="MarketExpress" onclick="parent.location.href='/OMAPX?userId=051220665&amp;clientId=8&amp;UserType=null&amp;BuyerCookie=null';">`

下面是我认为该按钮位于的表格:

 <div id="sidebar-left" height="50%" style="margin-right:10px">
<div id="bar" style="margin-right:-3px"><h1>Select To Order</h1></div>

<div style="width:100%; height:50%; border-left:1px solid #cccccc;border-right:1px solid #cccccc;border-bottom:1px solid #cccccc;margin-bottom:10px;"> 

 <table width="235px" cellspacing="0" cellpadding="0" border="0">

 <!--  <tr align="center">
 <td style="padding: 5px 40px 0px 40px;">


 <input class="crtordbtn" type="button" value="eSysco Express" id="esyscoExpress" onClick="parent.location.href='http://flex2.esysco.net';" />
 </td>
 </tr>
 <tr >
 <td style="padding: 5px 40px 0px 40px;text-align:left">
 <p>Our latest order management application with improved performance and enhanced usability</p>
</td>
</tr>-->

<tbody><tr align="center">

    <td style="padding: 5px 40px 0px 40px;">

            <input class="crtordbtn" type="button" value="Market Express" id="MarketExpress" onclick="parent.location.href='/OMAPX?userId=051220665&amp;clientId=8&amp;UserType=null&amp;BuyerCookie=null';">


    </td></tr>
    <tr>
    <td style="padding: 5px 40px 0px 40px;text-align:left">
        <p>Our latest order management application with improved performance and enhanced usability</p>
    </td></tr>
    <tr>



</tr>

</tbody></table>
 </div>

</div>

3 个答案:

答案 0 :(得分:1)

所需元素是动态元素,因此要定位该元素,必须使 WebDriverWait 成为可点击的 元素,并且可以使用以下任一解决方案:

  • 使用CSS_SELECTOR

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.crtordbtn#MarketExpress[onclick*='OMAPX?userId']"))).click()
    
  • 使用XPATH

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='crtordbtn' and @id='MarketExpress'][contains(@onclick, 'OMAPX?userId')]"))).click()
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

答案 1 :(得分:0)

尝试一下。应该可以。

driver.find_element_by_xpath("//table[@id='esyscoExpress']/tbody/tr//input[@class='crtordbtn']").click()

答案 2 :(得分:0)

尝试使用ID可能会起作用。希望对您有帮助

driver.find_element_by_id("MarketExpress").click()