![](/img/trans.png)
[英]find_element_by_xpath() shows syntax error using Selenium with Python
[英]SyntaxError: invalid syntax while using find_element_by_xpath using Selenium in 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")
我收到一個錯誤:
File "<ipython-input-32-592f058980cd>", line 5
driver.find_element_by_xpath("//*[@id="ddlHistorical"]").send_keys("NIFTY 100")
^
SyntaxError: invalid syntax
這個錯誤信息...
SyntaxError: invalid syntax
...暗示xpath表達式不是有效的xpath 表達式。
當您使用雙引號即"..."
作為xpath 時,您需要在單引號內提供屬性值,即'...'
。
所以你需要改變:
@id="ddlHistorical"
至:
@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); } } }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.