How to Access span element and change it's value

杀马特。学长 韩版系。学妹 提交于 2020-04-12 02:30:50

问题


I am trying to create a python + selenium script in order to fetch CSV from a salesforce pardot page.

Can Somebody please help me in accessing the nested span element inside the dropdown list which will be generated on button click.

I am adding my code done so far and I am getting Timeout Error While running my script.

from selenium import webdriver
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
try:
    chrome_options = webdriver.ChromeOptions()
    prefs = {'download.default_directory': r'C:\Pardot'}
    chrome_options.add_experimental_option('prefs', prefs)
    driver = webdriver.Chrome(executable_path="D:\XXX XXXX\XXXX\drivers\chromedriver.exe", options=chrome_options)
    driver.get('https://pi.pardot.com/engagementStudio/studio#/15627/reporting')
    user_name = driver.find_element_by_css_selector('#email_address')
    user_name.send_keys('XXXXXXXXXXXXXXXXXXX')
    password = driver.find_element_by_css_selector('#password')
    password.send_keys('XXXXXXXXXXXXXXXXX)
    submit_button = driver.find_element_by_css_selector('input.btn')
    submit_button.click()
    WebDriverWait(driver, 10).until(
    EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe#content-frame")))
    WebDriverWait(driver, 10).until(EC.text_to_be_present_in_element_value(text_='All Time',locator=(By.CSS_SELECTOR,"span[data-qa='reporting-filter-trigger-value']")))

except (RuntimeError) as e:
    print(e)
finally:
    time.sleep(10)
    driver.close()

Screenshot for span DOM element


Error Stack trace:

C:\Users\Vipul.Garg\AppData\Local\Programs\Python\Python37\python.exe "D:/Vipul Garg Backup/XXXX/testingCSVfetching.py"
Traceback (most recent call last):
  File "D:/Vipul Garg Backup/XXXX/testingCSVfetching.py", line 22, in <module>
    WebDriverWait(driver, 10).until(EC.text_to_be_present_in_element_value(text_='All Time',locator=(By.CSS_SELECTOR,"span[data-qa='reporting-filter-trigger-value']")))
  File "C:\Users\Vipul.Garg\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 

回答1:


try this approach

spans = driver.find_element_by_css_selector('.slds-button_reset.slds-grow.slds-has-blur-focus.trigger-button').get_attribute('innerHTML') print(spans) You should be getting all span.



来源:https://stackoverflow.com/questions/60501908/how-to-access-span-element-and-change-its-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!