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'
时间: 2024-01-28 19:05:00 浏览: 129
这个报错可能是因为在调用 `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]')`。
相关问题
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
这个报错可能是因为你在调用 `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]'))
```
这样就可以避免这个报错了。
优化这段代码driver = webdriver.Chrome() driver.get("http://cjwxb-test.dianpusoft.cn/userLogin") driver.get("http://cjwxb-test.dianpusoft.cn/new-interact/interact/setActivityTemplate?activityType=10021") time.sleep(3) driver.find_element(By.XPATH, '//*[@id="setActivityTemplate"]/div/div[2]/form/div[3]/div[1]/div').click() time.sleep(2) driver.find_element(By.XPATH, '//*[@id="setActivityTemplate"]/div/div[2]/form/div[3]/div[1]/div/div/button').click() time.sleep(2) driver.find_element(By.XPATH, '//*[@id="editTemplatePage"]/div/div[3]/div[2]/button[2]').click() driver.find_element(By.XPATH, '//*[@id="activitySetting1 dataForm"]/div/div[3]/form/div[1]/div/div/div[2]/div/input').clear() time.sleep(2) driver.find_element(By.XPATH, '//*[@id="activitySetting1 dataForm"]/div/div[3]/form/div[1]/div/div/div[2]/div/input').send_keys("AT") driver.find_element(By.XPATH, '//*[@id="activitySetting1 dataForm"]/div/div[3]/form/div[3]/div/div/div[2]/label[2]/span[1]/span').click() driver.find_element(By.XPATH, '//*[@id="activitySetting1 dataForm"]/div/div[3]/form/div[3]/div/div/div[3]/div[2]/label/span[1]/span').click() win32api.mouse_event(win32con.MOUSEEVENTF_WHEEL, 0, 0, -1000) driver.find_element(By.XPATH, '//*[@id="activitySetting1 dataForm"]/div/div[4]/div/form/div[1]/div[2]/div/div[1]/div/span[2]').click() driver.find_element(By.XPATH, '//*[@id="activitySetting1 dataForm"]/div/div[4]/div/form/div[2]/div/button/span').click()
可以尝试以下优化方式:
1. 使用显式等待代替 time.sleep(),可以更加精准地等待页面元素的加载,提高代码的执行效率。
2. 将重复的元素定位代码提取出来,封装成函数,减少代码冗余。
3. 将多个操作封装成一个函数,提高代码的可读性和可维护性。
下面是优化后的代码:
```python
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import win32api
import win32con
def wait_for_element(driver, locator):
return WebDriverWait(driver, 10).until(EC.presence_of_element_located(locator))
def click_element(driver, locator):
element = wait_for_element(driver, locator)
element.click()
def set_text(driver, locator, text):
element = wait_for_element(driver, locator)
element.clear()
element.send_keys(text)
def scroll_down(driver):
win32api.mouse_event(win32con.MOUSEEVENTF_WHEEL, 0, 0, -1000)
def set_activity_template(driver):
driver.get("http://cjwxb-test.dianpusoft.cn/new-interact/interact/setActivityTemplate?activityType=10021")
click_element(driver, (By.XPATH, '//*[@id="setActivityTemplate"]/div/div[2]/form/div[3]/div[1]/div'))
click_element(driver, (By.XPATH, '//*[@id="setActivityTemplate"]/div/div[2]/form/div[3]/div[1]/div/div/button'))
click_element(driver, (By.XPATH, '//*[@id="editTemplatePage"]/div/div[3]/div[2]/button[2]'))
set_text(driver, (By.XPATH, '//*[@id="activitySetting1 dataForm"]/div/div[3]/form/div[1]/div/div/div[2]/div/input'), "AT")
click_element(driver, (By.XPATH, '//*[@id="activitySetting1 dataForm"]/div/div[3]/form/div[3]/div/div/div[2]/label[2]/span[1]/span'))
click_element(driver, (By.XPATH, '//*[@id="activitySetting1 dataForm"]/div/div[3]/form/div[3]/div/div/div[3]/div[2]/label/span[1]/span'))
scroll_down(driver)
click_element(driver, (By.XPATH, '//*[@id="activitySetting1 dataForm"]/div/div[4]/div/form/div[1]/div[2]/div/div[1]/div/span[2]'))
click_element(driver, (By.XPATH, '//*[@id="activitySetting1 dataForm"]/div/div[4]/div/form/div[2]/div/button/span'))
driver = webdriver.Chrome()
driver.get("http://cjwxb-test.dianpusoft.cn/userLogin")
set_activity_template(driver)
```
阅读全文