実現したいこと
ウェブサイトのマウスを置いた際に表示される吹き出しの情報をCSVでまとめて出力したい。
発生している問題・分からないこと
Google Colaboratoryで
Google Chrome 122.0.6261.111
ChromeDriver 122.0.6261.111 (9d4c1072da62b411b351a38b9ed6214ab236aa7b-refs/branch-heads/6261@{#1015})
selenium(selenium-4.18.1-py3-none-any.whl)
を入れました。
のようにカーソルをボックスに当てて吹き出しで出てくる情報をまとめてcsv出力したいのですが、
以下のスクリプトで途中移行の要素が取得できません。
from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.select import Select import os,glob import urllib,requests,time from selenium.webdriver.common.by import By downloaddir = os.getcwd() options = webdriver.ChromeOptions() options.add_argument("--headless") options.add_argument('--disable-dev-shm-usage') options.add_argument("--no-sandbox") options.add_experimental_option("prefs", {"download.default_directory": downloaddir}) chrome = webdriver.Chrome(options=options) from bs4 import BeautifulSoup from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import pandas as pd pages = 1 chrome.execute_script("window.open('','_blank');") chrome.switch_to.window(chrome.window_handles[pages]) pages += 1 chrome.get("https://gisaid.org/lineage-comparison/") chrome.implicitly_wait(10) #iframe_name=chrome.find_element(by=By.TAG_NAME, value="iframe") chrome.switch_to.frame(0) var_select=chrome.find_element(by=By.XPATH, value="//*[@id='select-lineages']/div/div[2]/div[2]/select") for i in range(2): Select(var_select).select_by_index(i) create_rep=chrome.find_element(by=By.XPATH, value="//*[@id='app']/div/div[2]/div[1]/div[2]/div[1]/div[1]/div/button") create_rep.click() chrome.implicitly_wait(90) time.sleep(5) heatmap_select=chrome.find_element(by=By.XPATH, value="//*[@subtitle='S']/*[@id='heatmap']") heatmap_base=heatmap_select.find_elements(by=By.TAG_NAME, value="rect") print(len(heatmap_base)) actions = ActionChains(chrome) df_total=pd.DataFrame() hm_s=chrome.find_element(by=By.XPATH, value="//*[@id='mutation-heatmaps']/div[3]/div[3]/div/h4[text()='S']/..") chrome.switch_to.default_content() fullscrn=chrome.find_element(by=By.CLASS_NAME, value="fullscreenon") WebDriverWait(chrome,20).until(EC.element_to_be_clickable(fullscrn)).click() iframe=chrome.find_element(by=By.CLASS_NAME, value="a7iframe") chrome.switch_to.frame(iframe) chrome.maximize_window() chrome.execute_script("document.body.style.zoom= '25%';") chrome.save_screenshot("/content/drive/MyDrive/Docking/txt_screenshot.png") chrome.execute_script("document.body.style.zoom= '100%';") for k in heatmap_base: actions.move_to_element(k).perform() print(k.get_attribute("class")) chrome.implicitly_wait(2) # seconds heatmap_value=hm_s.find_element(by=By.ID, value="value") heatmap_mut=hm_s.find_element(by=By.ID, value="mutation") heatmap_var=hm_s.find_element(by=By.ID, value="lineage") hm_var=heatmap_var.text hm_mut=heatmap_mut.text hm_val=heatmap_value.text df_var=pd.Series([hm_var,hm_mut,hm_val]) df_total=pd.concat([df_total,df_var],axis=1) print(df_var) chrome.execute_script('window.scrollBy(0.5, 0);') chrome.execute_script("document.body.style.zoom= '25%';") chrome.save_screenshot("/content/drive/MyDrive/Docking/scroll_screenshot.png") chrome.execute_script("document.body.style.zoom= '100%';") df_total=df_total.T df_total.reset_index(drop=True) output = os.path.join('/content/drive/MyDrive/Docking/', 'total_var_EG5.csv') df_total.to_csv(output, index=False)
エラーメッセージ
error
1(結果) 2![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2024-03-18/58554093-57ed-41ab-b016-3400951756bc.jpeg) 3ポインターは動いているものの、 4```ここに言語を入力 5 heatmap_value=hm_s.find_element(by=By.ID, value="value") 6 heatmap_mut=hm_s.find_element(by=By.ID, value="mutation") 7 heatmap_var=hm_s.find_element(by=By.ID, value="lineage")
が空で出力されていません。
### 該当のソースコード
特になし
### 試したこと・調べたこと - [x] teratailやGoogle等で検索した - [x] ソースコードを自分なりに変更した - [ ] 知人に聞いた - [ ] その他 ##### 上記の詳細・結果 (試したこと) 1. chrome.execute_script("document.body.style.zoom= '25%';") で画面上に全てのボックスを表示させるとポインターのout of rangeエラー。 2. ポインター移動後にtime.sleep(2)を入れても結果に変化なし 3. for文の最後にchrome.execute_script('window.scrollBy(0.5, 0);')を入れて少しずつ画面を右にスクロールさせても結果のスクリーンショットで画面移動されておらず。 ### 補足 特になし
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。