SyntaxError:在Python中使用Selenium使用find_element_by_xpath时语法无效

2024-09-30 08:36:33 发布

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

代码测试:

from selenium import webdriver
driver = webdriver.Firefox()
driver.get("http://www.niftyindices.com/reports/historical-data")
driver.maximize_window()
driver.find_element_by_xpath("//*[@id="ddlHistorical"]").send_keys("NIFTY 100")

我收到一个错误:

^{pr2}$

Tags: 代码fromimportcomhttpdatagetwww
2条回答

此错误消息。。。在

SyntaxError: invalid syntax

…意味着xpath表达式不是有效的xpath表达式。在

当您使用双引号"..."时,您需要在单引号中提供属性值,即'...'。在

所以你需要改变:

^{pr2}$

收件人:

@id='ddlHistorical'

有效的代码行:

driver.find_element_by_xpath("//*[@id="ddlHistorical"]").send_keys("NIFTY 100")

将是:

driver.find_element_by_xpath("//*[@id='ddlHistorical']").send_keys("NIFTY 100")

在这种情况下,不能使用发送键从下拉框中选择值:

import java.util.List; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.Test; public class Testing { public static WebDriver driver; @Test public void test() throws InterruptedException { System.setProperty("webdriver.chrome.driver", "./Driver/chromedriver"); driver = new ChromeDriver(); driver.get("http://www.niftyindices.com/reports/historical-data"); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS); driver.findElement(By.xpath("//*[@id=\"HistoricalData\"]/div[1]/div/div/a")).click(); Thread.sleep(2000); List<WebElement> elements = driver.findElements(By.xpath("//*[@id=\"mCSB_2_container\"]/li")); for (WebElement element : elements) { String mCSB = element.getText(); if (mCSB.equalsIgnoreCase("NIFTY 100")) { element.click(); } System.out.println(mCSB); } } }

和13;
和13;

相关问题 更多 >

    热门问题