在python selenium中与动态标签交互元素 [英] interact elements with dynamic label in python selenium

查看:31
本文介绍了在python selenium中与动态标签交互元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是 Python 新手.全部1个月大.我试图抓取页面中的大多数元素,我能够处理它们并与它们交互.有两个元素具有我无法处理的动态标签.源页面如下

Python newbie here. All of 1 month old. Most of the elements in page i am trying to scrape, i am able to handle them and interact with them. there are two elements that have dynamic labels which i am not able to handle. The source page looks as below

<span class="a-button-inner">
   <input class="a-button-input" type="submit" aria-labelledby="Ae8MCi-55">
   <span id="Ae8MCi-55" class="a-button-text" aria-hidden="true">Add Your Key</span>
</span>

每次刷新时,标签都是新的,因此我无法获得固定的 XPATH.有多个跨类为a-button-inner"的项目.以及输入类a-button-input"页面的其余部分还有其他提交按钮.唯一独特的是跨度文本添加您的密钥".

Upon each refresh the label is new so i cannot get a fixed XPATH of that. There are multiple items with span class "a-button-inner" as well as input class "a-button-input" and there are other submit buttons as well in the rest of the page. The only thing unique is the Span text "Add Your Key".

感谢所有帮助获取提交按钮元素并单击/提交它.

Appreciate all help to get the submit button element and to click/submit it.

from selenium import webdriver
.
.
.
.
# objAddKey = driver.find_element_by_xpath('//*[@id="Ae8MCi-55"]/span/input') does not work as second round its a different XPath
objAddKey = driver.find_element_by_link_text('Add Your Key') # hoping this will get a sibling and then to get the parent and then look for all the children.  I don't even know if its possible in python.
objAddKey.click()'

尝试通过跨文本搜索,并遇到了其他一些东西,https://www.tutorialspoint.com/how-to-get-text-found-between-span-selenium

Tried to search for searching by span text, and came across some other stuff, https://www.tutorialspoint.com/how-to-get-text-found-between-span-selenium

WebElement l = driver.findElement(By.xpath("//p/span"));
String s = l.getText();

但这也无济于事.

https://selenium-python.readthedocs.io/locating-elements.html经历了这个,但也没有太大帮助.

https://selenium-python.readthedocs.io/locating-elements.html went through this, but not much help either.

感谢您的帮助和指点.

谢谢.

推荐答案

如果 Add Your Key 是唯一的,那么你可以使用下面的 xpath :

If Add Your Key is unique then you can use the below xpath :

//span[contains(text(), 'Add Your Key')]//preceding-sibling::input[@class='a-button-input']

甚至没有课:

//span[contains(text(), 'Add Your Key')]//preceding-sibling::input

并像这样使用它:

objAddKey = driver.find_element_by_xpath("//span[contains(text(), 'Add Your Key')]//preceding-sibling::input")
objAddKey.click()

我建议您进行显式等待以获得更高的可靠性.

I would suggest you to have explicit waits for more reliability.

Selenium - Python - 显式等待

这篇关于在python selenium中与动态标签交互元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆