GVKun编程网logo

如何使用Selenium和Python获取内部具有动态部分的选择器?(selenium获取动态元素)

10

对于想了解如何使用Selenium和Python获取内部具有动态部分的选择器?的读者,本文将提供新的信息,我们将详细介绍selenium获取动态元素,并且为您提供关于PythonSelenium使用小

对于想了解如何使用Selenium和Python获取内部具有动态部分的选择器?的读者,本文将提供新的信息,我们将详细介绍selenium获取动态元素,并且为您提供关于Python Selenium 使用小 HTML 查找具有动态 URL 和名称的元素、使用Selenium和Python列出选择选项值、使用Selenium和python在textBox中快速编写、使用selenium和python实现web站点敏捷自动化测试的有价值信息。

本文目录一览:

如何使用Selenium和Python获取内部具有动态部分的选择器?(selenium获取动态元素)

如何使用Selenium和Python获取内部具有动态部分的选择器?(selenium获取动态元素)

我的应用程序有很多选择器,里面有动态ID。当该动态ID位于选择器的末尾时,我将[id^=''staticPart'']在每个选择器的内部使用。例如:

#tab-10 > svg > tspan

变成:

[id^=''tab-''] > svg > tspan

我工作得很好,但是我不知道如何处理这样的选择器:

#tab-t0-1

其中0是动态数字,其他所有内容都是静态的。我正在尝试以下操作,但收到无效的选择器错误:

[id^=''tab-'']-1

答案1

小编典典

要标识具有动态元素,id例如 #tab-t0-1 ,其中 0 是动态数字,其他所有元素都是静态的,则可以将 cssSelector
与以下通配符结合使用:

  • ^ :表示属性值

  • $ :表示属性值

因此,最精细的定位器将包括寻找 初始字母(tab-t)结尾字母(-1)的策略, 并且应为:

[id^=''tab-t''][id$=''-1'']

Python Selenium 使用小 HTML 查找具有动态 URL 和名称的元素

Python Selenium 使用小 HTML 查找具有动态 URL 和名称的元素

如何解决Python Selenium 使用小 HTML 查找具有动态 URL 和名称的元素?

大家好!今天我正在使用 Python 3 和 Selenium Web 驱动程序。我在按元素查找时遇到问题。这是 HTML:

<a _ngcontent-pxc-c302=""href="/person/20d4a795d3fb43bdbee7e480df27b05b">michele regina</a>

目标是点击出现在列出的列中的名字。

名称随着每次页面刷新而变化。这带来了两个问题,即链接文本是名称并且它会发生变化,并且除了显示 /person/

的部分之外,每个不同名称的链接也会不断变化

我尝试了以下方法:

driver.find_element_by_css_selector("a.name ng-star-inserted").click()
driver.find_element_by_class_name("name ng-star-inserted").click()

这导致元素不是可点击错误。 我还通过复制和粘贴从 Google Inspector 复制的整个 XPath 来尝试 xpath...lol

    driver.find_element_by_xpath(''/html/body/app-root/app-container/div/div/tc-app-view/div/div/div/ng-component/mat-sidenav-container/mat-sidenav-content/div/div[2]/tc-person-table-container/tc-collapse-expand[1]/section/tc-person-table/table/tbody/tr[1]/td[2]/tc-person-table-cell/a'').click()

它有时会起作用,这很奇怪,但我确信必须有更好的方法。非常感谢您!任何帮助表示赞赏!另外,我对 Python 非常陌生,我知道零 HTML,而且我也是堆栈的新手!谢谢!

解决方法

从给定的完整 xpath 中,您要查找的 a 标记位于 table,section,div 中。在这些标签中查找 ID 并跟踪 a

使用Selenium和Python列出选择选项值

使用Selenium和Python列出选择选项值

我有以下HTML代码

<select name="countries" class_id="countries">    <option value="-1">--SELECT COUNTRY--</option>    <option value="459">New Zealand</option>    <option value="100">USA</option>    <option value="300">UK</option></select>

我正在尝试使用Selenium获取选项值的列表(例如459、100等,而不是文本)。

目前,我有以下Python代码

from selenium import webdriverdef country_values(website_url):    browser = webdriver.Firefox()    browser.get(website_url)    html_code=browser.find_elements_by_xpath("//select[@name=''countries'']")[0].get_attribute("innerHTML")    return html_code

如您所见,代码返回纯HTML,我正在使用HTMLParser库进行解析。有什么方法可以仅使用Selenium来获取选项值?换句话说,不必解析Selenium的结果吗?

答案1

小编典典

检查一下,这是我做的,然后才知道选择模块做了什么

from selenium import webdriverbrowser = webdriver.Firefox()#code to get you to the pageselect_box = browser.find_element_by_name("countries") # if your select_box has a name.. why use xpath?..... # this step could use either xpath or name, but name is sooo much easier.options = [x for x in select_box.find_elements_by_tag_name("option")]# this part is cool, because it searches the elements contained inside of select_box # and then adds them to the list options if they have the tag name "options"for element in options:    print element.get_attribute("value")     # or append to list or whatever you want here

这样的输出

-1459100300

使用Selenium和python在textBox中快速编写

使用Selenium和python在textBox中快速编写

我正在使用Selenium和Python(Chorme驱动程序)在文本框中编写内容,但是有很多文本框,我需要它来更快地填充它们。我使用一系列

driver.find_element_by_xpath("//input[@and @id='order_billing_name']").send_keys("test.com")

命令,但是写10-11这些则要花费很多时间。有办法加快速度吗?

使用selenium和python实现web站点敏捷自动化测试

使用selenium和python实现web站点敏捷自动化测试

需求

对web站点实现功能自动化测试,提高测试效率

实施

开发语言:Python3

工具:Selenium

浏览器:以Chrome为例,安装浏览器驱动ChromeDriver,测试其他浏览器下载对应驱动即可

源码

https://gitee.com/shcsong/automate

关于如何使用Selenium和Python获取内部具有动态部分的选择器?selenium获取动态元素的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于Python Selenium 使用小 HTML 查找具有动态 URL 和名称的元素、使用Selenium和Python列出选择选项值、使用Selenium和python在textBox中快速编写、使用selenium和python实现web站点敏捷自动化测试等相关内容,可以在本站寻找。

本文标签: