Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Selenium Python返回UID属性值_Python_Selenium_Xpath_Css Selectors_Getattribute - Fatal编程技术网

使用Selenium Python返回UID属性值

使用Selenium Python返回UID属性值,python,selenium,xpath,css-selectors,getattribute,Python,Selenium,Xpath,Css Selectors,Getattribute,我有一个类似这样的站点,我想使用firefox+selenium+python提取uid字段的内容。每页只有一个UID <div class= "parent" > <div class="module profile" dcp="1" uid="90"> ... ... </div> </div> 但他们都无法返回UID。我要么得到一个空集,要么只得到类(即整个模块类,而不是DIV中的属性) 我看到一些人推荐get_属性

我有一个类似这样的站点,我想使用firefox+selenium+python提取uid字段的内容。每页只有一个UID

<div class= "parent" >
   <div class="module profile" dcp="1" uid="90">
   ...
   ...
   </div>
</div>
但他们都无法返回UID。我要么得到一个空集,要么只得到类(即整个模块类,而不是DIV中的属性)

我看到一些人推荐get_属性选择器,但我没有成功地将其应用到这个用例中


感谢您的指导。

要提取属性uid的值,即文本90,您可以使用以下任一选项:

  • css\u选择器

    myText = browser.find_element_by_css_selector("div.parent>div.module.profile").get_attribute("uid")
    
    myText = WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.parent>div.module.profile"))).get_attribute("uid")
    
  • xpath

    myText = browser.find_element_by_xpath("//div[@class='parent']/div[@class='module profile']").get_attribute("uid")
    
    myText = WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='parent']/div[@class='module profile']"))).get_attribute("uid")
    
但是,属性uid,即文本90似乎是一个动态元素,因此必须诱导WebDriverWait使元素可见,并且可以使用以下任一解决方案:

  • css\u选择器

    myText = browser.find_element_by_css_selector("div.parent>div.module.profile").get_attribute("uid")
    
    myText = WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.parent>div.module.profile"))).get_attribute("uid")
    
  • xpath

    myText = browser.find_element_by_xpath("//div[@class='parent']/div[@class='module profile']").get_attribute("uid")
    
    myText = WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='parent']/div[@class='module profile']"))).get_attribute("uid")
    
注意:您必须添加以下导入:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

请提供您尝试的selenium脚本。整个脚本没有很多有用的信息。我相信我只需要一行选择代码就可以得到这个。但是,我尝试使用诸如
浏览器之类的工具。通过类名称(“模块”)查找元素[0]
,但这会将整个元素返回到。还尝试了
browser.find\u elements\u by\u xpath(“module.profile”)
,但这会再次返回div的子级,而不是初始div块中的属性。谢谢