python selenium get 按钮 class 然后点击
python selenium get button by class and click
我在 Python 中使用 selenium。该页面有一个带有 class 关联的按钮。我使用 class 获取元素,但它失败了。请检查下面的代码
<div class="form-cell">
<button type="submit" class="btn btn-orange">Sign Up</button>
</div>
到目前为止已经尝试了以下...并且每次都出现相同的错误。实际代码中前三行有注释,请注意。
driver.find_element_by_class_name('btn btn-orange').click()
driver.find_elements_by_class_name('btn btn-orange').click()
driver.find_element_by_xpath('//button[@type="button"]/[@class="btn btn-orange"]').click()
driver.find_element_by_xpath("//button[@type='button']/option[text()='Sign Up']").click()
以上所有尝试都显示相同的错误消息。
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//button[@type='button']/option[text()='Sign Up']"}
(Session info: chrome=91.0.4472.77)
我已经设法识别文本框并向其中输入某些数据。但现在我正在尝试提交该数据,但似乎无法抓住按钮来点击它。
感谢您的帮助
试试这个:
driver.find_element_by_css_selector('.btn.btn-orange').click()
find_element_by_class_name()
没有正确处理 class 名称中的空格。
如果你想使用XPath,我想应该是这样的
driver.find_element_by_xpath('//button[@class="btn btn-orange"]').click()
试试这个 xpath:
.//button[@type='submit' and text()='Sign Up']
您分享的 html 中没有 'button' 类型和文本选项
试试这个:
driver.find_element_by_xpath('//button[@type="submit" and (contains(@class,"btn-orange")) and(contains(text(),"Sign Up"))]').click()
这应该能 100% 捕捉到它;)
我在 Python 中使用 selenium。该页面有一个带有 class 关联的按钮。我使用 class 获取元素,但它失败了。请检查下面的代码
<div class="form-cell">
<button type="submit" class="btn btn-orange">Sign Up</button>
</div>
到目前为止已经尝试了以下...并且每次都出现相同的错误。实际代码中前三行有注释,请注意。
driver.find_element_by_class_name('btn btn-orange').click()
driver.find_elements_by_class_name('btn btn-orange').click()
driver.find_element_by_xpath('//button[@type="button"]/[@class="btn btn-orange"]').click()
driver.find_element_by_xpath("//button[@type='button']/option[text()='Sign Up']").click()
以上所有尝试都显示相同的错误消息。
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//button[@type='button']/option[text()='Sign Up']"}
(Session info: chrome=91.0.4472.77)
我已经设法识别文本框并向其中输入某些数据。但现在我正在尝试提交该数据,但似乎无法抓住按钮来点击它。
感谢您的帮助
试试这个:
driver.find_element_by_css_selector('.btn.btn-orange').click()
find_element_by_class_name()
没有正确处理 class 名称中的空格。
如果你想使用XPath,我想应该是这样的
driver.find_element_by_xpath('//button[@class="btn btn-orange"]').click()
试试这个 xpath:
.//button[@type='submit' and text()='Sign Up']
您分享的 html 中没有 'button' 类型和文本选项
试试这个:
driver.find_element_by_xpath('//button[@type="submit" and (contains(@class,"btn-orange")) and(contains(text(),"Sign Up"))]').click()
这应该能 100% 捕捉到它;)