from selenium.webdriver.common.by import By ele = self.find_element(By.XPATH, '//*[@id="app"]/div/div[2]/section/div/div[1]/div/div[3]/div[1]') 报错:TypeError: find_element() takes 2 positional arguments but 3 were given
时间: 2024-01-20 20:02:19 浏览: 149
这个报错可能是因为你在调用 `find_element` 方法时,将 `By.XPATH` 和选择器字符串作为两个独立的参数传递给了该方法,而 `find_element` 方法只接受一个参数,即一个元组类型的 `locator`。
你可以尝试修改代码,将 `By.XPATH` 和选择器字符串作为一个元组传递给 `find_element` 方法,例如:
```
from selenium.webdriver.common.by import By
ele = self.find_element((By.XPATH, '//*[@id="app"]/div/div[2]/section/div/div[1]/div/div[3]/div[1]'))
```
这样就可以避免这个报错了。
相关问题
from selenium.webdriver.common.by import By locator=By.XPATH,'//*[@id="app"]/div/div[2]/section/div/div[1]/div/div[3]/div[1]' ele = self.find_element(locator=locator) 报错:AttributeError: 'tuple' object has no attribute 'value'
这个报错可能是因为在调用 `find_element` 方法时传入的 `locator` 参数是一个元组类型,而 `find_element` 方法中要求传入的 `locator` 参数应该是一个 `By` 类型和一个字符串类型的元组,例如 `(By.XPATH, '//*[@id="app"]/div/div[2]/section/div/div[1]/div/div[3]/div[1]')`。
你可以尝试修改代码,将 `locator` 参数改为正确的格式,即 `(By.XPATH, '//*[@id="app"]/div/div[2]/section/div/div[1]/div/div[3]/div[1]')`。
public static void main(String[] args){ System.setProperty("webdriver.firefox.bin","C:\Users\Y\Desktop\软件测试\实验二\实验相关\FirefoxChina_13.0.1\Mozilla Firefox\firefox.exe"); WebDriver driver = new FirefoxDriver(); Navigation na = driver.navigate(); na.to("https://shz.58.com/ershoufang/"); driver.findElement(By.id("search-input")).clear(); List<WebElement> ele = driver.findElements(By.cssSelector("h3.property-content-title-name"));//爬取房屋名字 List<WebElement> ele1s=new ArrayList<>(); List<WebElement> ele3s=new ArrayList<>(); for(int j=1;j<=10;j++){ String s1="//div[@id='__layout']/div/section/section[3]/section/section[2]/div["+j+ "]/a/div[2]/div/section/div[@class='property-content-info']/p[2]"; String s3="//div[@id='__layout']/div/section/section[3]/section/section[2]/div["+j+ "]/a/div[2]/div/section/div[@class='property-content-info property-content-info-comm']/p[2]"; WebElement ele11 = driver.findElement(By.xpath(s1));//爬取房屋面积信息 WebElement ele33 = driver.findElement(By.xpath(s3));//爬取房屋地址信息 ele1s.add(ele11); ele3s.add(ele33); } List<WebElement> ele2 = driver.findElements(By.cssSelector("p.property-price-total"));//爬取房屋价格信息 for(int i=0;i<ele1s.size();i++){ System.out.println("名字:"+ele.get(i).getText()); System.out.println("地址:"+ele3s.get(i).getText()); System.out.println("面积:"+ele1s.get(i).getText()); System.out.println("价格:"+ele2.get(i).getText()); System.out.println(); } }
这是一个 Java 程序,主要是使用 Selenium WebDriver 来爬取 58 同城二手房的房屋信息。程序的主要流程是:
1. 设置 Firefox 浏览器的路径;
2. 创建 FirefoxDriver 对象;
3. 使用 navigate() 方法访问 58 同城二手房的页面;
4. 清空搜索框中的内容;
5. 使用 findElements() 方法找到所有的房屋名称、面积、地址和价格信息;
6. 使用 for 循环遍历每个房屋的信息,并打印出来。
需要注意的是,程序中使用了多个 XPath 表达式来定位元素,这些表达式需要根据实际页面结构进行修改。另外,程序中只爬取了页面上的前 10 个房屋信息,如果需要爬取更多的信息,需要修改 for 循环的终止条件。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![nupkg](https://img-home.csdnimg.cn/images/20250102104920.png)
![nupkg](https://img-home.csdnimg.cn/images/20250102104920.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pptx](https://img-home.csdnimg.cn/images/20241231044947.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)