gpt4 book ai didi

python - 脚本无法通过 Selenium 和 Python 使用 xpath 或 id 或其他任何内容找到按钮

转载 作者:行者123 更新时间:2023-12-01 08:22:08 25 4
gpt4 key购买 nike

我对使用 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>

最佳答案

所需的元素是动态元素,因此要找到该元素,您必须引发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

关于python - 脚本无法通过 Selenium 和 Python 使用 xpath 或 id 或其他任何内容找到按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54563453/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com