我尝试单击imagen之类的按钮。
无法使用类或xpath
这是试图单击按钮的代码:
from bokeh.plotting import figure, ColumnDataSource
from bokeh.embed import components
from bokeh.models import LinearAxis, Range1d, HoverTool
def convert_to_categorical(months):
month_names = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
return [month_names[m-1] for m in months]
def NetRevGraph(NetRev, Vol):
# bar chart (hovering works fine)
x1 = [int(i['Month']) for i in NetRev]
y1 = [i['Amount'] for i in NetRev]
m1 = convert_to_categorical(x1)
tooltip_net_rev = [("Net Revenue", "$y"),]
source_net_rev = ColumnDataSource(data=dict(x=m1, y=y1, ))
p = figure(plot_width=500, plot_height=400, x_range=m1, tooltips=tooltip_net_rev)
p.vbar(x='x', width=0.5, bottom=0, top='y', color="#B3DE69", source=source_net_rev)
# add line graph (can seem to complete this so that hover works)
x2 = [int(i['MonthNum']) for i in Vol]
y2 = [i['Val'] for i in Vol]
m2 = convert_to_categorical(x2)
source_vol = ColumnDataSource(data=dict(x=m2, y=y2, ))
tooltip_net_vol = [("Volume", "$y"), ]
p.extra_y_ranges = {"Vol": Range1d(start=min(y2), end=max(y2))}
p.add_tools()
p.line(x='x', y='y', line_width=2, y_range_name="Vol", color="black", source=source_vol)
p.add_layout(LinearAxis(y_range_name="Vol"), 'right')
这是之前的xpath:
driver.findElement(By.xpath("/html/body/div[10]/button")).click();
请有人可以帮助我!
答案 0 :(得分:1)
页面中的每个简单更改都会导致您的代码停止运行,请尝试始终使用 class 或 id 并导航至其同级节点/父节点。 / p>
您可以通过两种方式做到这一点
1。使用CssSelector(在DevTools->复制->复制选择器中右键单击元素)
driver.FindElement(By.CssSelector("CopiedText")).Click()
2。通过使用XPath并通过其父项访问它(针对您的案例)
driver.FindElement(By.xpath("//div[@class='advertising-layer']/button")).Click()
答案 1 :(得分:0)
在访问对话框中的按钮时遇到了类似的问题。我尝试使用ID为XPath的XPath无效,但可以与 CSS选择器一起使用。 通过使用CSS选择器,我使硒Web驱动程序单击了一个按钮,从对话框的下拉菜单中选择一个值,在文本框中键入一个值。 我不确定为什么它可以与CSS选择器一起使用,而不与XPath一起使用。如果有人对此有描述,我将不胜感激。
如果要获取特定元素的CSS选择器,请执行以下步骤
右键单击代码的突出显示部分,然后将鼠标悬停在副本上,您将找到CSS选择器
让我知道这是否对您有用。
答案 2 :(得分:0)
根据您共享的 HTML ,您可以在所需元素上调用click()
来诱导 WebDriverWait ,并且可以使用以下解决方案:>
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("/div[@class='advertising-mask']//button"))).click();