Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.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获取元素属性时遇到问题_Python_Python 3.x_Selenium_Selenium Webdriver_Selenium Chromedriver - Fatal编程技术网

使用Selenium | Python获取元素属性时遇到问题

使用Selenium | Python获取元素属性时遇到问题,python,python-3.x,selenium,selenium-webdriver,selenium-chromedriver,Python,Python 3.x,Selenium,Selenium Webdriver,Selenium Chromedriver,每次我尝试获取aria标签属性时,我都会遇到这个问题,这里的问题根本没有帮助。我试过睡觉,并列出理解(我真的不知道为什么这会起作用),但这就是我现在所处的位置,我的大脑是果冻:)。如果这是您想要建议的,那么不能使用xpath messages = driver.find_elements_by_class_name('DMBLb') buttons = message.find_elements_by_class_name('wpO6b') for button in buttons:

每次我尝试获取aria标签属性时,我都会遇到这个问题,这里的问题根本没有帮助。我试过睡觉,并列出理解(我真的不知道为什么这会起作用),但这就是我现在所处的位置,我的大脑是果冻:)。如果这是您想要建议的,那么不能使用xpath

messages = driver.find_elements_by_class_name('DMBLb')

buttons = message.find_elements_by_class_name('wpO6b')

for button in buttons:
   option_buttons = button.get_attribute('aria-label') 
   print(option_buttons) # returns none


根据您的问题和您共享的HTML,该元素似乎是一个React元素,因此要检索属性aria标签,您必须输入WebDriverWait以使所需元素可见,并且您可以使用以下解决方案:

 print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "element_xpath_you_found"))).get_attribute("aria-label"))

尝试等待列表中的所有元素。为此,您需要导入一些selenium模块:

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

wait = WebDriverWait(driver, 30)
wait.until(EC.visibility_of_all_elements_located(SelectBy.CLASS_NAME("wpO6b")))

buttons = message.find_elements_by_class_name('wpO6b')

for button in buttons:
   option_buttons = button.get_attribute('aria-label') 

还有,你想打印什么?尝试
选项按钮。文本

1。你看到了什么错误?2.将html代码或链接添加到您正在测试的站点。您写道:如果您想建议使用xpath,则不能使用xpath。但已接受xpath的答案。给我一个webdriverwait未定义错误,你确定这不是JavaScript吗?你需要从selenium.webdriver.support.ui导入一些函数从selenium.webdriver.common.by导入WebDriverWait从selenium.webdriver.support导入预期条件作为ECNice idea复制我的答案首先,这不是你的主意,单击参考,看看这是谁的主意;)