如何使用Selenium WebDriver和Python选择选项?

2024-05-06 22:47:42 发布

您现在位置:Python中文网/ 问答频道 /正文

如何使用Selenium WebDriver和Python选择选项:

有人有解决办法吗?

我用这个来获取select元素:

try:
    FCSelect = driver.find_element_by_id('FCenter')
    self.TestEventLog = self.TestEventLog + "<br>Verify Form Elements: F Center Select found"
except NoSuchElementException:
    self.TestEventLog = self.TestEventLog + "<br>Error: Select FCenter element not found"

是否有类似于“getFirstSelectedOption”的内容:

try:
    FCenterSelectedOption = FCenterSelect.getFirstSelectedOption()
    self.TestEventLog = self.TestEventLog + "<br>Verify Form Elements: F Center Selected (First) found"
except NoSuchElementException:
    self.TestEventLog = self.TestEventLog + "<br>Error: Selected Option element not found"

然后我想用类似于getText的方法验证内容:

try:
    FCenterSelectedOptionText = FCenterSelectedOption.getText()
    self.TestEventLog = self.TestEventLog + "<br>Verify Form Elements: FCenter Selected Option Text found"
except NoSuchElementException:
    self.TestEventLog = self.TestEventLog + "<br>Error: Selected Option Text element not found"

if FCenterSelectedOptionText == 'F Center Option Text Here':
    self.TestEventLog = self.TestEventLog + "<br>Verify Form Elements: F Center Selected Option Text found"
else:
    self.TestEventLog = self.TestEventLog + "<br>Error: F Center 'Selected' Option Text not found"

Tags: textbrselfformnoterrorelementselement