Preto Preto - 2 months ago 12
Python Question

Click button (python), having problems with both mechanize and selenium

I'm trying to click the "Coevolution Scores (TXT)" button that pops up after clicking the "Export..." button, however, using a python script here, I could not do it either with mechanize nor selenium, using different selection types.

Is there any other way, or can you possibly figure out what I am doing wrong?

I used this code:

url="http://polyview.cchmc.org/cgi-bin/coevolve.cgi?JOB=c8a266e0d7ba7cc"
driver = webdriver.Chrome(executable_path="C:/ProgramFiles/Google/Chrome/Application/chromedriver.exe")
driver.get(url)
time.sleep(15)
driver.find_element(by=By.XPATH, value="//button[@title='Export...']").click()


also tried by link_text and by putting
find_element_by_link_text
, instead of putting the argument inside. Below is the button I try to click.

<form id="downloadMat" target="formTarget" method="POST" action="coeviz_data.pl">
<input name="x" class="idData" type="hidden" value="683436.chi">
<input name="dl" class="dlData" type="hidden" value="683436.chi.wph">
<input name="w" class="weighted" type="hidden" value="wph">
<input name="res" type="hidden" value="scores.txt">
<button onclick="submit()">Coevolution Scores (TXT)</button>
</form>

Answer

The code you are trying to select is inside an <iframe> element. This means that you have to switch frame first before selecting anything that is inside it. This will work:

from selenium import webdriver
import time
from selenium.webdriver.common.by import By
url="http://polyview.cchmc.org/cgi-bin/coevolve.cgi?JOB=c8a266e0d7ba7cc"
driver = webdriver.Chrome(executable_path="C:/ProgramFiles/Google/Chrome/Application/chromedriver.exe")
driver.get(url)
time.sleep(15)
driver.switch_to_frame('ifCoeViz')
driver.find_element(by=By.XPATH, value="//button[@title='Export...']").click()