我在 python 中使用 selenium,现在我想通过它的 id 名称的一部分来定位一个元素,我该怎么办?
I'm using selenium with python,now I want to locate an element by part of its id name,what can I do?
例如,现在我已经通过 id 名称 coption5 找到了一个项目:
For example,now I've already located a item by id name coption5 :
sixth_item = driver.find_element_by_id("coption5")
有没有我只能使用 coption 来定位这个元素?
Is there anyway I can locate this element only by using coption?
找到你找到的元素:
sixth_item = driver.find_element_by_id("coption5")
要仅使用 coption 定位此元素,您可以使用以下任一 定位器策略:
To locate this element only by using coption you can use can use either of the following Locator Strategies:
使用 XPATH 和 starts-with():
sixth_item = driver.find_element_by_xpath("//*[starts-with(@id, 'coption')]")
使用 XPATH 和 contains():
sixth_item = driver.find_element_by_xpath("//*[contains(@id, 'coption')]")
使用 CSS_SELECTOR 和 ^(开头的通配符):
Using CSS_SELECTOR and ^ (wildcard of starts-with):
sixth_item = driver.find_element_by_css_selector("[id^='coption']")
使用 CSS_SELECTOR 和 *(包含通配符):
Using CSS_SELECTOR and * (wildcard of contains):
sixth_item = driver.find_element_by_css_selector("[id*='coption']")
您可以在以下位置找到关于动态 CssSelectors 的详细讨论:
You can find a detailed discussion on dynamic CssSelectors in:
这篇关于如何使用python在selenium中通过其id名称的一部分查找元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
如何在python中的感兴趣区域周围绘制一个矩形How to draw a rectangle around a region of interest in python(如何在python中的感兴趣区域周围绘制一个矩形)
如何使用 OpenCV 检测和跟踪人员?How can I detect and track people using OpenCV?(如何使用 OpenCV 检测和跟踪人员?)
如何在图像的多个矩形边界框中应用阈值?How to apply threshold within multiple rectangular bounding boxes in an image?(如何在图像的多个矩形边界框中应用阈值?)
如何下载 Coco Dataset 的特定部分?How can I download a specific part of Coco Dataset?(如何下载 Coco Dataset 的特定部分?)
根据文本方向检测图像方向角度Detect image orientation angle based on text direction(根据文本方向检测图像方向角度)
使用 Opencv 检测图像中矩形的中心和角度Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 检测图像中矩形的中心和角度)