我有一個帶有代碼名稱和 ID 的資料框,從這里我必須首先選擇扇區名稱,然后選擇我嵌套回圈的符號,然后我強制通過資料框代碼與結果表匹配彈出。
for sec in sector.options[1:]:
sec.click()
for sym in symbol.options:
table = sym.text
for x in range(len(df)):
if df['Ticker'][x] == table:
#SEARCH ELEMENT BY TEXT AND THEN CLICK ON IT
#text= find_element_by_css_selector('span:contains(df['Ticker'][x])')
#text = find_element_by_xpath('//span[contains(text(), df['Ticker'][x]]').click()
我的問題是在 if 陳述句之后,我希望能夠搜索元素文本,然后一個一個地單擊每個匹配的代碼,然后從彈出的表中復制資料。我正在嘗試使用 find_element_by_xpath 但我不知道如何在這里使 contains(text()) 位 python 可格式化?任何幫助,將不勝感激
在您端運行的代碼:
chromedriver = "chromedriver.exe"
driver = webdriver.Chrome(chromedriver)
driver.get("https://www.psx.com.pk/psx/resources-and-tools/listings/listed-companies")
driver.maximize_window()
sector = Select(driver.find_element_by_id("sector"))
symbol = Select(driver.find_element_by_id("Symbol"))
for sec in sector.options[1:]:
sec.click()
for sym in symbol.options:
table = sym.text
for x in range(len(df)):
if df['Ticker'][x] == table:
#element1 = find_element_by_css_selector('span:contains("View All Companies")') #How to make this click on the text in the table
#element1 = find_element_by_xpath('//span[contains(text(), df]')
driver.close
uj5u.com熱心網友回復:
您可以定位addressbookdataid,因為它在 HTML 中是唯一的,然后get_attribute呼叫innerText
代碼:
wait = WebDriverWait(driver, 30)
print(wait.until(EC.presence_of_element_located((By.ID, "addressbookdata"))).get_attribute('innerText'))
進口:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/460058.html
標籤:Python python-3.x 硒 网页抓取
