简体   繁体   English

如何选择具有此功能的 div

[英]how do I select the div that has this <i> using Selenium python

I want to scrape a quiz with selenium using python, here's the code for 1 answer choice.我想使用 python 用 selenium 进行测验,这是 1 个答案选择的代码。

the will contains this specific class when it's correct answer , and when its not correct it is another class for this当它是正确答案时,遗嘱包含这个特定的类,当它不正确时,它是另一个类

so I want to get the text of the question where the answer is correct (The product's impact on customer satisfaction.) It will depends of the class inside the < i > (because as I said, when its false answer, it's another class inside )所以我想得到答案正确的问题文本(产品对客户满意度的影响。)这将取决于 < i > 里面的类(因为正如我所说,当它的错误答案时,它是里面的另一个类)

But I can't find how to select it with XPATH但我找不到如何使用 XPATH 选择它

<div class='course-player__interactive-checkbox _interactive-checkbox_cjbh9b'>
  <div class='course-player__interactive-checkbox__choice-label _interactive-checkbox__choice-label_cjbh9b'>
    <span class='course-player__interactive-checkbox__choice-letter _interactive-checkbox__choice-letter_cjbh9b'> ANSWER A </span>
  </div>
  <span class='course-player__interactive-checkbox__choice-text _interactive-checkbox__choice-text_cjbh9b'> The product's impact on customer satisfaction. </span>
  <span class='course-player__interactive-checkbox__choice-answered _interactive-checkbox__choice-answered_cjbh9b'>
    <i class='toga-icon toga-icon-circle-fill-check _interactive-checkbox__choice-icon_cjbh9b'> </i>
  </span>
</div>

One approach to retrieve the string一种检索字符串的方法

The product's impact on customer satisfaction.产品对客户满意度的影响。

with Selenium is the following: Selenium 如下:

from selenium import webdriver
driver = webdriver.Chrome()
driver.get("file:///input.html")
lookup = driver.find_elements_by_xpath("//div/span[i]/preceding-sibling::*[1]")
print(lookup[0].text)

The XPath-1.0 expression selects the <span> before the <span> which has an <i> child. XPath-1.0 表达式选择<span>之前的<span> ,它有一个<i>子节点。
But be aware that this code does not contain any error handling.但请注意,此代码不包含任何错误处理。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 python 从 selenium 的下拉列表中 select 项目? - How do I select an item from dropdown in selenium using python? 如何使用Python Selenium选择表中的元素? - How do I select an element within a table using Python Selenium? 如何通过在 selenium 中搜索找到 div 元素,然后使用 selenium 和 python 从该 div 复制属性? - how do i find a div element by searching in selenium, and then copying an attribute from that div using selenium and python? 使用 Python 和 Beautifulsoup 如何在 div 中使用 select 所需的表? - Using Python and Beautifulsoup how do I select the desired table in a div? 如何在 Selenium WebDriver 的下拉 div 菜单中 select 一个元素? (Python) - How do I select an element in dropdown div menu in Selenium WebDriver? (Python) 如何选择具有该名称的Microsoft CRM表单上的Last Name元素 <input id = “lastname_i”…> 使用Python selenium驱动程序? - How do I select the Last Name element on Microsoft CRM form that has the name <input id = “lastname_i”…> using Python selenium driver? 如何在Selenium中使用Python定位具有特定样式的div? - How do I target a div with a certain style with Selenium in Python? 如何使用python selenium单击div容器流体中包含的链接 - How do I click on links that are contained in a div container fluid using python selenium 如何使用 Python 中的 Selenium 从在滚动上添加 div 的网页中抓取数据? - How do I scrape data using Selenium in Python from a webpage that adds div on scroll? 如何在 Python 中检查表单是否有错误? (硒) - How do I check if form has error in Python? (selenium)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM